Search code examples
javascriptmongodbmongo-shell

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 ?


Solution

  • 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.