MomentJS和UTC日期处理

MomentJS and UTC date handling

本文关键字:处理 日期 UTC MomentJS      更新时间:2023-09-26

有人能解释为什么MomentJS在调用moment().utc("2014-09-22T00:00:00").toDate()时返回值Fri Oct 24 2014 06:06:44 GMT-0400 (Eastern Standard Time)吗?文件可在http://momentjs.com/docs/#/parsing/utc/但没有什么能清楚地表明我为什么会得到这些结果。

可运行的代码示例位于http://jsfiddle.net/o9jqy2qo/2/--这是相同的代码:

var dateStr = "2014-09-22T00:00:00";
var jsDateInfo = new Date(dateStr);
var momentLocalInfo = moment(dateStr).toDate();
var momentUtcInfo1 = moment(dateStr).utc().toDate();
var momentUtcInfo2 = moment().utc(dateStr).toDate();
alert('dateStr = ' + dateStr + ''n' + 'new Date(dateStr) = ' + new Date(dateStr) + ''n' + 'moment(dateStr).toDate() = ' + moment(dateStr).toDate() + ''n' + 'moment(dateStr).utc().toDate() = ' + moment(dateStr).utc().toDate() + ''n' + 'moment().utc(dateStr).toDate() = ' + moment().utc(dateStr).toDate() + ''n');

我知道获得我期望的结果的正确方法是调用moment("2014-09-22T00:00:00").utc().toDate()。我只是想了解这里发生了什么。

对我来说,它返回GMT-0600(MST)。在不提供时区或偏移量的情况下,moment假设本地时间偏移量作为浏览器配置的一部分。

埋在当下.js文档:

除非指定时区偏移量,否则解析字符串将在当前时区中创建日期。