[SERVER-21350] new Date function is not working Created: 09/Nov/15  Updated: 09/Jul/16  Resolved: 18/Nov/15

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 3.2.0-rc2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: carl dong Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change
Operating System: ALL
Steps To Reproduce:

mongo --nodb

MongoDB shell version: 3.2.0-rc2
> new Date('2015-11-01 00:00:00')
ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ")

Participants:

 Description   

Can't get date value with new Date() function with mongo shell client 3.2.0 rc2.

MongoDB shell version: 3.2.0-rc2
> new Date('2015-11-01 00:00:00')
ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ")

Which should return ISODate("2015-10-31T16:00:00Z") .



 Comments   
Comment by carl dong [ 09/Nov/15 ]

Thanks , I am going to modify the date format with "T".

Comment by Stennie Steneker (Inactive) [ 09/Nov/15 ]

Hi Carl,

Thanks for reporting!

It looks like this is a parsing difference between the V8 JavaScript engine used in prior releases and the Spidermonkey engine used in MongoDB 3.2.

The ECMAScript 5 Date constructor only defines support for RFC-2822 or ISO8601 date formats. The spec says that "other formats may be used, but results may be unexpected".

If you use the ISO8601 formats of YYYY-MM-DD (just date) or YYYY-MM-DDThh::mm::ss (date and time) you should get the expected result. Note that ISO8601 requires a "T" as a delimiter within a date and time string:

> new Date('2015-11-01')
ISODate("2015-11-01T00:00:00Z")
 
> new Date('2015-11-01T00:00:00')
ISODate("2015-11-01T07:00:00Z")

If you aren't able easily change the format of your date strings, as a workaround I would suggest using the ISODate() constructor in the mongo shell:

> new ISODate('2015-11-01 00:00:00')
ISODate("2015-11-01T00:00:00Z")

The ISODate() function is a shell helper which was added to implement ISO date parsing in MongoDB before ES5-compliant JavaScript engines added support for ISO date strings in JavaScript's built-in Date() constructor.

Regards,
Stephen

Generated at Thu Feb 08 03:57:06 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.