Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting ISODate from Mongodb

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

Which looks like this

ISODate("2012-07-14T01:00:00+01:00") 

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.

like image 840
jamjam Avatar asked Jul 14 '12 19:07

jamjam


People also ask

Does MongoDB convert date to UTC?

MongoDB stores times in UTC by default, and will convert any local time representations into this form. Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.

How do I read ISODate files?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).

How do I change the ISO date format?

The ISO date format For example, "3rd of April 2002", in this international format is written: 2002-04-03 .


1 Answers

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.

like image 165
Sarah Roberts Avatar answered Sep 28 '22 19:09

Sarah Roberts