MomentJS Timezone is returning incorrect date and time

独自空忆成欢 提交于 2020-01-24 20:06:37

问题


I am using moment.js and moment-timezone-with-data.min.js to calculate the date time.

The date / time I am using is as follows:

var dGC2018 = moment('2018-04-04T18:00:00').tz("Australia/Queensland");

The date is 4th of April 2018. The time is 6PM. The timezone is Queensland.

The output (using console.log) is:

Thu Apr 05 2018 04:00:00 GMT+1000 (E. Australia Standard Time) {}

This output can be found by outputting dCG2018 to the console.log and expanding the returned object to find the _d property.

Why is momentjs stating the incorrect date / time relative to my input parameters?


回答1:


You have to use moment.tz instead of tz function. moment.tz parses your input string using the given timezone (in your case 'Australia/Queensland').

You are getting the wrong output because you are parsing a string using local timezone (as stated here: By default, moment parses and displays in local time.) and then converting it to 'Australia/Queensland' timezone.

Moreover, you do not have to use _d and other internal propertries to get the value of moment objects. As stated in the Internal Properties section of momentjs official guide:

Moment objects have several internal properties that are prefixed with _.

The most commonly viewed internal property is the _d property that holds the JavaScript Date that Moment wrappers. Frequently, developers are confused by console output of the value of _d. Moment uses a technique called epoch shifting that causes this property to sometimes differ from the actual date value that the Moment reflects. In particular if Moment TimeZone is in use, this property will almost never be the same as the actual value that Moment will output from its public .format() function. As such, the values of _d and any other properties prefixed with _ should not be used for any purpose.

To print out the value of a Moment, use .format(), .toString() or .toISOString().

Here a working sample:

var dGC2018 = moment.tz('2018-04-04T18:00:00', "Australia/Queensland");
console.log(dGC2018.format())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"></script>



回答2:


I think the offset is probably not accounted for here. Try using the below code snippet and see if it works for you, modify the dateValue and moment time zone value to match your needs:

var localTimeZoneMoment = moment(dateValue);
var userTimeZoneMoment = localTimeZoneMoment.clone();
userTimeZoneMoment.tz('Australia/Queensland');
userTimeZoneMoment.add(localTimeZoneMoment.utcOffset() - userTimeZoneMoment.utcOffset(), 'minutes');

var  formattedLocalTime = moment.tz(userTimeZoneMoment, 'Australia/Queensland').format('ddd, DD-MMM-YYYY hh:mm a')


来源:https://stackoverflow.com/questions/45602092/momentjs-timezone-is-returning-incorrect-date-and-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!