简体   繁体   中英

Convert utc date to local date using momentjs

I have a DateTime string like 2/24/2017 17:00:00 which comes from a web service. The time is UTC time. Now i want to convert it to user's local time and display it in browser. And i'm using Moment.js .

I've tried:

var utcTime= moment('2/24/2017 17:00:00' + " +0000", "MM/DD/YYYY HH:mm:ss Z");

var localTime = utcTime.add(new Date().getTimezoneOffset().toString(), 'm').toDate();

It will work but somehow looks strange. Am i missing something? Is there a better way to do this?

Write it like this:

moment.utc('2/24/2017 17:00:00', "MM/DD/YYYY HH:mm:ss").local().format('lll');

Reference: http://momentjs.com/

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