How to convert from UTC local time in moment.js?

I have a simple usage example, but I cannot figure out how to convert from GMT / UTC to local time in moment.js.

Example:

var gmtDateTime = moment.utc("2015-10-24 20:00", "YYYY-MM-DD HH").format('YYYY-MMM-DD h:mm A'); 

console.log(gmtDateTime) emits 2015-Oct-24 8:00 PM , which is correct. Now I just want to convert this to my local time, which happens to be Mountain Daylight Time. Therefore, the correct conversion date will be 2015-Oct-24 2:00 PM , because I am 6 hours earlier than GMT / UTC.

How can this be done simply with a moment. js? (THANKS!)

+5
source share
1 answer

Try moment().local() .

Example:

 var gmtDateTime = moment.utc("2015-10-24 20:00", "YYYY-MM-DD HH") var local = gmtDateTime.local().format('YYYY-MMM-DD h:mm A'); 
+11
source

Source: https://habr.com/ru/post/1234440/


All Articles