Formatting ISODate from Mongodb


In Mongodb I am storing date and time in ISODate format.

Which looks like this


Using nodejs/javascript, how can I display the time component so I would get something like this

Time : 01:00

I am using momentjs to make this easier but from what I can tell momentjs does seem to support the ISODate format.

Thanks for you help.

7/14/2012 7:35:07 PM

Accepted Answer

JavaScript's Date object supports the ISO date format, so as long as you have access to the date string, you can do something like this:

> foo = new Date("2012-07-14T01:00:00+01:00")
Sat, 14 Jul 2012 00:00:00 GMT
> foo.toTimeString()
'17:00:00 GMT-0700 (MST)'

If you want the time string without the seconds and the time zone then you can call the getHours() and getMinutes() methods on the Date object and format the time yourself.

7/14/2012 10:32:58 PM

MongoDB's ISODate() is just a helper function that wraps a JavaScript date object and makes it easier to work with ISO date strings.

You can still use all of the same methods as working with a normal JS Date, such as:


// Note that getHours() and getMinutes() do not include leading 0s for single digit #s

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow