new Date(..).getTime() 不等于 momentJS 中的 moment(..).valueOf()?

new Date(..).getTime() is not equal to moment(..).valueOf() in momentJS?

new Date(..).getTime() 应该 return 一个以毫秒为单位的时间戳。根据 documentation of momentJS 表达式 moment(..).valueOf() 应该执行相同的操作 (return 给定日期的时间戳(以毫秒为单位).

我检查了以下示例数据:

var timeStampDate = new Date("2015-03-25").getTime(); //timestamp in milliseconds?
> 1427241600000
var timeStampMoment = moment("03-25-2015", "MMDDYYYY").valueOf(); //timestamp in milliseconds?
> 1427238000000

如您所见,结果并不相同。

现在我正在 momentJS 中搜索一个函数,return 对我来说与表达式 new Date(..).getTime().[=15 完全相同的数据=]

日期构造函数doc:

The UTC time zone is used to interpret arguments in ISO 8601 format that do not contain time zone information

矩构造​​函数doc:

Unless you specify a timezone offset, parsing a string will create a date in the current timezone

因此在 moment 构造函数中指定时区会产生与日期相同的行为:

var timeStampMoment = moment("03-25-2015 +0000", "MM-DD-YYYY Z").valueOf(); //> 1427241600000

当您将相同的值传递给 Date 和 moment 时(至少在 Chrome 几年后),您会从这两个值中获得相同的值。

new Date("2015-03-25").getTime()
1427241600000
moment("03-25-2015", "MMDDYYYY").valueOf()
1427259600000
new Date("03-25-2015").getTime()
1427259600000

您实际命中的只是对 Date.parse

中日期格式的不同猜测