GETah
GETah

Reputation: 21409

Creating UTC time in momentjs is returning weird results

I am tearing my hear off on a Monday evening! Can anyone please help me understand the problem below? I have the following string on my browser 01-09-2014 (this is in local time), I want to convert this into UTC so that I can save that back to the server. Here is what I am doing:

    var localDate = moment("01-09-2014", "DD-MM-YYYY");
    var utcDate = localDate.utc();
    console.log("Local Date " + localDate.toDate() + " UTC Date " + utcDate.toDate());

Strangely enough, the last line outputs:

Local Date Mon Sep 01 2014 00:00:00 GMT+0200 (Romance Daylight Time) UTC Date Mon Sep 01 2014 00:00:00 GMT+0200 (Romance Daylight Time) 

QUESTION

Could you please help me with these two questions?

Upvotes: 1

Views: 928

Answers (1)

conceptdeluxe
conceptdeluxe

Reputation: 3893

Thoug I am not sure about ... but I think it should be:

var utcDate = localDate.utc().format()

See:

http://momentjs.com/docs/#/parsing/utc/

UPDATED

Fiddle:

http://jsfiddle.net/qhk9tnLr/2/

JS:

console.log("Local Date: " + moment("01-09-2014", "DD-MM-YYYY").toDate());
console.log("UTC Date: " + moment("01-09-2014", "DD-MM-YYYY").utc().format("ddd MMM DD YYYY HH:mm:ss zZZ"));

Output:

Local Date: Mon Sep 01 2014 00:00:00 GMT+0200
UTC Date: Sun Aug 31 2014 22:00:00 UTC+0000

Conclusion:

toDate() will give the local time - to get UTC use utc().format()

Upvotes: 3

Related Questions