简体   繁体   中英

Date to UTC in momentjs

I am trying to convert my date to UTC using momentjs:

var some_date = 06/06/2016
var new_date = moment(new Date(some_date)).utc().format("YYYY-MM-DD HH:mm");
console.log(new_date)

2016-06-06 04:00

My date is still returned in EDT... Can someone help me get it to show in UTC instead?

You can use the utc() method of moment when creating:

var some_date = '06/06/2016';
console.log(moment.utc(new Date(some_date)).format('YYYY-MM-DD HH:mm'))
console.log(moment(new Date(some_date)).format('YYYY-MM-DD HH:mm'));

EDIT: best practice based on @Maggie's comment

 console.log(moment.utc(some_date, 'MM/DD/YYYY').format('YYYY-MM-DD HH:mm'));
 console.log(moment(some_date, 'MM/DD/YYYY').format('YYYY-MM-DD HH:mm'));

Outputs:

2016-06-06 04:00
2016-06-06 00:00

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