【问题标题】:Moment.js diff is returning zero even when there is a difference即使存在差异,Moment.js 差异也会返回​​零
【发布时间】:2019-09-04 07:27:11
【问题描述】:

我试图在几分钟内获得两个日期对象之间的差异。问题是当日期对象相同时,它返回零。但是当差异变为 1 时,差异也返回零。

我的代码如下

let currentDate = Moment();
let takenDateString = `${currentDateString(date)} ${hours}:${minutes} ${isAM ? 'AM' : 'PM'}`
let takenDate = Moment(takenDateString, 'YYYY-MM-DD hh:mm A');

console.log("=======================")
console.log(currentDate.format('YYYY-MM-DD hh:mm A'))
console.log(takenDateString);
console.log(takenDate.diff(currentDate, "minutes"));
console.log("=======================")

这也是日志中打印的内容

Util.js:62 =======================
Util.js:63 2019-09-04 03:26 PM
Util.js:64 2019-09-04 03:26 PM
Util.js:65 0
Util.js:66 =======================
Util.js:62 =======================
Util.js:63 2019-09-04 03:26 PM
Util.js:64 2019-09-04 03:26 PM
Util.js:65 0
Util.js:66 =======================
Util.js:62 =======================
Util.js:63 2019-09-04 03:26 PM
Util.js:64 2019-09-04 03:27 PM
Util.js:65 0
Util.js:66 =======================
Util.js:62 =======================
Util.js:63 2019-09-04 03:26 PM
Util.js:64 2019-09-04 03:28 PM
Util.js:65 1
Util.js:66 =======================

您会看到上面的行为。即使时差为 1,它也返回 0,当它为 2 时,它返回 1。为什么会出现这种奇怪的行为?

【问题讨论】:

  • 你可以试试:takenDate.diff(currentDate, "minutes", true) 这将返回一个浮点数而不是一个整数。
  • 会不会是以秒为单位的差异,小于60秒的都算0分钟?
  • 答案取决于预期结果。 2019-09-04 03:27:00.001 - 2019-09-04 03:26:59.999 算不算相差一分钟?

标签: javascript date ecmascript-6 momentjs


【解决方案1】:

查看 Moment 中 diff 函数的 source code,我们看到它采用分钟差的绝对值并将其降低。比如0.5分钟变成0,1.9分钟变成1。

将第三个参数作为true 传递给diff() 会给你一个浮点数的答案(可以是负数)。

let a = moment("2019-09-04T09:33:30.000");
let b = moment("2019-09-04T09:34:00.000");
a.diff(b, "minutes") // -> 0
a.diff(b, "minutes", true) // -> -0.5

如果您想要“时钟分钟数”的差异,您可以使用minutes()

let a = moment("2019-09-04T09:33:30.000");
let b = moment("2019-09-04T09:34:00.000");
b.minutes() - a.minutes() // -> 1

【讨论】:

    【解决方案2】:

    时刻截断(即向下舍入)差异(因此 59 秒将变为 0 分钟)。

    https://momentjs.com/docs/#/displaying/difference/

    【讨论】:

      【解决方案3】:

      您应该在将 takeDateString 传递给时刻之前将其转换为日期对象。

      编辑:您也可以使用字符串,但转换为日期对象对我有用。

      let currentDate = Moment();
      let takenDateString = `${currentDateString(date)} ${hours}:${minutes} ${isAM ? 'AM' : 'PM'}`
      let takenDate = Moment(new Date(takenDateString), 'YYYY-MM-DD hh:mm A'); // pass a date object instead
      
      console.log("=======================")
      console.log(currentDate.format('YYYY-MM-DD hh:mm A'))
      console.log(takenDateString);
      console.log(takenDate.diff(currentDate, "minutes"));
      console.log("=======================")
      

      【讨论】:

      • 不需要将其转换为日期。 Moment.js 也可以使用字符串:momentjs.com/docs/#/parsing/string-format
      • @LuudvanKeulen 当然可以,但为什么要投反对票?我尝试了这个解决方案并且它有效。你试过了吗?
      • 它们都工作正常。不管是字符串还是日期。
      猜你喜欢
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 2011-01-05
      • 2020-10-17
      • 2021-06-29
      • 1970-01-01
      • 2013-03-20
      • 2020-04-14
      相关资源
      最近更新 更多