[SERVER-22925] ISODate from object key in shell Created: 02/Mar/16  Updated: 23/Mar/16  Resolved: 23/Mar/16

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 3.2.1
Fix Version/s: None

Type: Bug Priority: Trivial - P5
Reporter: Eric Stobbart Assignee: Mira Carey
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

> var date = ISODate();
> var o = {};
> o[date] = true;
> ISODate(Object.keys(o)[0]); // fails..
> new Date(Object.keys(o)[0]); // works, and returns an ISODate

Sprint: Platforms 12 (04/01/16)
Participants:

 Description   

I'm trying to make a unique set of ISODate's, the doc's mention ISODate being a wrapper around `new Date()`. When they're used as object keys they get converted to strings (as expected). If I try to put that string back through the constructor function the iso regex fails.. `throw Error("invalid ISO date");`.



 Comments   
Comment by Eric Stobbart [ 23/Mar/16 ]

Jason,
Thanks.
I guess I was hoping `toString()` might actually route to `toISOString()` so it would be more intuitive to make the round trip, or support the toString value in the constructor.
So best practice would be to deserialize differently, that makes sense.
Thanks again.

-Eric

Comment by Mira Carey [ 23/Mar/16 ]

Eric,

ISODate only accepts ISO 8601 style date strings. I.e. YYYY-MM-DDTHH:MM:SS... style strings.

It in turn returns regular JavaScript Date's. Those Date's have a toString() function which returns something localized like "Wed Mar 23 2016 14:01:24 GMT-0400 (EDT)", which you've noticed cannot be used as the argument to ISODate.

If you'd like something that roundtrips, you can take advantage of the other serialization we offer for date's: date.tojson(). It's non-intuitive, but that's actually the serialization routine called when you default print objects in the shell, which I imagine is where the confusion is coming from.

To use your example:

> var date = ISODate();
> var o = {};
> o[date.tojson()] = true;
> ISODate(Object.keys(o)[0]); // works now

Comment by Kelsey Schubert [ 04/Mar/16 ]

Hi estobbart,

Thank you for reporting this behavior. I am sending this ticket to the platform team; please continue to watch this ticket for updates.

Kind regards,
Thomas

Generated at Thu Feb 08 04:01:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.