Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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.

2 Answers

0 votes
by (106k points)

To format ISODate from Mongodb 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)'

0 votes
by (108k points)

Basically in MongoDB, Date() function is having 4 formats to format the ISODate:

  1. new Date("<YYYY-mm-dd>")

The above code will return the ISODate with the date that you have specified.

  1. new Date("<YYYY-mm-ddTHH:MM:ss>") 

The above function will define the date and the time in the users' local timezone and it will return the ISODate with the defined datetime in UTC(Universal Time Coordinated).

  1. new Date("<YYYY-mm-ddTHH:MM:ssZ>") 

The above approach will define the date and the time in Universal Time Coordinated and it will return the ISODate with the specified date and time in UTC(Universal Time Coordinated).

  1. new Date(<integer>) 

The above code defines the date and the time in milliseconds because of the Unix epoch (Jan 1, 1970), and it will return the resulting ISODate state.

Related questions

0 votes
2 answers
0 votes
2 answers
0 votes
2 answers
0 votes
1 answer

Browse Categories

...