Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-354

ISODate not working as expected

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 0.5.0
    • Affects Version/s: None
    • Component/s: Shell API
    • None
    • 3
    • Not Needed
    • Iteration Nectarine

      ISODate is used to parse the date from an ISO 8601 string and uses UTC as default time zone if the string does not specify one:

      > new ISODate("1977-04-01T00:00:00")
      ISODate("1977-04-01T00:00:00Z")
      
      > ISODate('2020-09-16T12:41:51.405+0200')
      ISODate("2020-09-16T10:41:51.405Z")
      

      ISODate also can't parse other formats:

      > new ISODate("1/4/1977")
      2020-09-16T14:53:59.162+0200 E  QUERY    [js] uncaught exception: Error: invalid ISO date: 1/4/1977 :
      ISODate@src/mongo/shell/types.js:79:15
      @(shell):1:1
      
      Date instead is working like in other javascript environments, can parse all sort of strings but uses the current timezone instead of UTC if the argument does not specifies one explicitly:
      > new Date("1977-04-01T00:00:00")
      ISODate("1977-03-31T23:00:00Z")
      

      In mongosh however it looks like we treat ISODate like the default javascript Date
      (notice how the parsing produces an incorrect date as it uses the current timezone):

      new ISODate("1977-04-01T00:00:00")
      1977-03-31T23:00:00.000Z
      

      NOTE: as part of this ticket it would be good to also align the format of Date and ISODate to be copy/paste friendly, like we do
      for the other BSON types:

      > new ISODate("1977-04-01T00:00:00")
      ISODate("1977-04-01T00:00:00Z")
      

      and

      > new Date("1977-04-01T00:00:00")
      ISODate("1977-04-01T00:00:00Z")
      

            Assignee:
            anna.henningsen@mongodb.com Anna Henningsen
            Reporter:
            maurizio.casimirri@mongodb.com Maurizio Casimirri
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: