gpt4 book ai didi

javascript - MomentJS max 和 min 返回错误值

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:15 24 4
gpt4 key购买 nike

在使用 max 和 min 方法比较两个 MomentJS 对象时,我遇到了一个奇怪的结果。他们似乎返回了错误的值。例如,此代码今天而不是明天返回:

moment().max(moment().add(1, 'd'))

http://jsfiddle.net/cGtbY/

谁能解释这种行为?

最佳答案

您误解了 min 的含义和 max .

来自测试套件(https://github.com/moment/moment/blob/develop/test/moment/min_max.js#L51):

    equalMoment(test, now.max(future), now,   "Now with the maximum of the future should be now");

意思的理解方式是:a.max(b) <= b (最迟,结果可以是第二次约会)。

文档有明确的引用:

Sometimes, server clocks are not quite in sync with client clocks. This ends up displaying humanized strings such as "in a few seconds" rather than "a few seconds ago". You can prevent that with moment#max()

.max因此函数是数值最小值(选择较早的时刻)

关于javascript - MomentJS max 和 min 返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19355230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com