本文介绍了如何禁用moment.js夏时制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在moment.js中禁用夏令时转换?

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

基本上,我的应用程序仅处理事件和日期,但是moment.js转换夏时制导致日期问题.它有任何设置会在整个应用程序使用中禁用它吗?

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?

推荐答案

如果您要说的是时刻显示日期和时间(以UTC表示,如"Z"所示),那么您就是应该使用moment.utc:

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"

像现在一样,当使用默认的矩量构造函数时,您在告诉矩量将UTC时间转换为本地时间,这就是为什么看到时差的原因.例如,在我的本地计算机上(我现在是UTC-5),我得到以下信息:

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"

这个问题很多,所以我写了这篇博客文章,解释了矩的构造函数以及它如何转换ISO8601日期的详细信息: https://maggiepint.com/2016/05/14/moment-js-shows-the-wrong-date/

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/

这篇关于如何禁用moment.js夏时制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:50