Difference between moment.utc(date) and moment(date).utc()

一曲冷凌霜 提交于 2019-12-11 02:34:25

问题


Trying to understand the behaviour and difference between:

moment.utc(date) and moment(date).utc()

Using '2018-05-31' as a param:

moment.utc('2018-05-31').format() will give:

‌2018-05-31T00:00:00Z

while moment('2018-05-31').utc().format() will give:

2018-05-31T04:00:00Z

I am executing both in EST timezone.


回答1:


The first moment.utc(String) parses your string as UTC, while the latter converts your moment instance to UTC mode.

By default, moment parses and displays in local time.

If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().

This brings us to an interesting feature of Moment.js. UTC mode.

See Local vs UTC vs Offset guide to learn more about UTC mode and local mode.

console.log( moment.utc('2018-05-31').format() );
console.log( moment('2018-05-31').utc().format() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>


来源:https://stackoverflow.com/questions/50608132/difference-between-moment-utcdate-and-momentdate-utc

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