Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Date and ISO Date conversion in Mongo shell

I was trying mongoExport with some date condition, I read here that date has to be in epoch format.

Question is,

I tried below,

> new Date(2013,10,16)
ISODate("2013-11-16T00:00:00Z")

Assuming that I gave Oct-16-2013, But it returned me '2013-11-16'. Same with epoch format also.

> new Date(2013,10,16)*1
1384560000000
> 
> new Date(1384560000000)
ISODate("2013-11-16T00:00:00Z")

Can you please help, why its changing the month to 11 ?

like image 881
Srivatsa N Avatar asked Jan 10 '14 00:01

Srivatsa N


People also ask

How do I change the ISO date in MongoDB?

You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range 0 through 9999 to the new Date() constructor or the ISODate() function. These functions accept the following formats: new Date("<YYYY-mm-dd>") returns the ISODate with the specified date.

What is ISO date in MongoDB?

ISODate("2012-12-19T06:01:17.171Z") ISODate() is a helper function that's built into to MongoDB and wraps the native JavaScript Date object. When you use the ISODate() constructor from the Mongo shell, it actually returns a JavaScript Date object.

How do I change the date format in MongoDB aggregation?

If you have documents that store dates as Date objects, but you want to return them in a different format, you can use the $dateToString aggregate pipeline operator. The $dateToString operator converts the Date object to a string, and optionally allows you to specify a format for the resulting output.


1 Answers

The month parameter of JavaScript's Date constructor is 0-based. So 0 for January to 11 for December, making it 9 for October instead of 10.

like image 87
JohnnyHK Avatar answered Nov 14 '22 22:11

JohnnyHK