Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
149
rated 0 times [  155] [ 6]  / answers: 1 / hits: 18575  / 6 Years ago, wed, may 30, 2018, 12:00:00

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.


More From » momentjs

 Answers
14

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>




[#54315] Saturday, May 26, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
virginia

Total Points: 632
Total Questions: 95
Total Answers: 95

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;