简体   繁体   中英

MomentJS and UTC date handling

Can anybody explain why MomentJS returns a value of Fri Oct 24 2014 06:06:44 GMT-0400 (Eastern Standard Time) when moment().utc("2014-09-22T00:00:00").toDate() is called? The documentation is available at http://momentjs.com/docs/#/parsing/utc/ but nothing there gave me a clear indication of why I'd get those results.

A runnable code sample is up at http://jsfiddle.net/o9jqy2qo/2/ -- here's that same code:

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');

I know that the correct way to get the results I'm expecting is to call moment("2014-09-22T00:00:00").utc().toDate() . I just want to understand what's going on here.

For me, it returns GMT-0600 (MST). Without providing a timezone or offset, moment assumes the local time offset as asserted as part of your browser configuration.

Buried in the moment.js docs :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM