Tallboy
Tallboy

Reputation: 13407

Why does moment.js return this date wrong?

I'm simply trying to take an input string and convert it to a date object.

moment.utc('2000-01-01T00:00:00.000Z').toDate()

But it returns this...

Fri Dec 31 1999 19:00:00 GMT-0500 (EST)

Thanks

Upvotes: 1

Views: 3609

Answers (2)

Benjamin Dean
Benjamin Dean

Reputation: 149

That is a valid JavaScript Date Object. You can test this by opening a console in Chrome or Firefox, then entering the following:

// Mon Nov 24 2014 09:54:00 GMT-0800 (PST) (looks the same as your example)
console.log( new Date() );

If you want to format the value coming out of moment.js, you can use its format method and a mask.

// Example: November 24th 2014, 09:58:12am
var fdate = moment().format('MMMM Do YYYY, h:mm:ss a');

Moment.js doesn't modify the prototype, it simply wraps it.

If you want to convert a string to a date object using moment.js, you can just call it as such:

moment(your_date); // Unless in UTC mode this will display as local time

In your instance you're using the UTC mode.

Upvotes: 1

edouard
edouard

Reputation: 152

2000-01-01T00:00:00.000Z is the GMT date/time.

Using moment.utc("2000-01-01T00:00:00.000Z").toDate() returns this date/time according to your timzone settings.

See : http://www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/

Hope it helps.

Upvotes: 0

Related Questions