简体   繁体   中英

DateTime convert from UTC to other timezone

I am using below code to convert date time to america/Chicago but getting wrong converted time.

process.env.TZ = "UTC";
var moment = require('moment-timezone');
var startTime = (new Date('2016-04-08 17:40:04')).toISOString();s
var newDateTime = moment(startTime);
var newDateTime = newDateTime.tz('America/Chicago').format('YYYY-MM-DD HH:MM:ss a');
console.log(newDateTime); // expected 2016-04-08 12:40:04 PM
                          // getting  2016-04-08 12:04:04 pm

You use the wrong format string, MM means Month number , you need to use mm - Minutes .

var newDateTime = newDateTime.tz('America/Chicago').format('YYYY-MM-DD HH:mm:ss a');

Documentation

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