Trung Tran
Trung Tran

Reputation: 13771

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?

Upvotes: 0

Views: 2694

Answers (1)

mcgraphix
mcgraphix

Reputation: 2733

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

Upvotes: 3

Related Questions