简体   繁体   中英

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

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.

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> 

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