how to disable moment.js daylight timezone conversion

孤街浪徒 提交于 2020-01-14 19:26:07

问题


It is possible to disable the daylight timezone conversion in moment.js?

http://plnkr.co/edit/MjFelt?p=preview

      $scope.obj.date = moment('2016-06-03T04:00:00.000Z');

Basically my application deals with events and dates only, but moment.js converting the daylight savings time is causing issue with dates. Does it have any setting which will disable it across the entire application usage?


回答1:


If you are saying that you want moment to display your date and time (which is UTC, as indicated by the 'Z'), exactly as is, you should use moment.utc:

moment.utc('2016-06-03T04:00:00.000Z').format()
"2016-06-03T04:00:00Z"

When you use the default moment constructor, as you are now, you are telling moment to convert your UTC time to local time, and this is why you are seeing a time difference. For instance, on my local machine (I am currently UTC-5) I get the following:

moment('2016-06-03T04:00:00.000Z').format()
"2016-06-02T23:00:00-05:00"

This question comes up quite a lot, so I wrote this blog post that explains moment's constructor functions and how it converts ISO8601 dates in detail: https://maggiepint.com/2016/05/14/moment-js-shows-the-wrong-date/



来源:https://stackoverflow.com/questions/37797893/how-to-disable-moment-js-daylight-timezone-conversion

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