Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-40727

$dateToString %G format specifier results in unexpected year

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.9
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Query
    • ALL

      $dateToString with the %G format specifier results in an unexpected year value. %Y seems normal.

      // Date()
      > new Date(1546300799999)
      ISODate("2018-12-31T23:59:59.999Z")
       
      // $dateToString with %G
      > db.test.aggregate([{$project:{date: {$dateToString: {format:"%G-%m-%d %H:%M:%S.%L", date:{$toDate: 1546300799999}}}}}])
      { "_id" : ObjectId("5cbf7ca5e176254eb1d8be0c"), "date" : "2019-12-31 23:59:59.999" }
      
      original description

      When dealing with datetimes specified as milliseconds since the Unix epoch, it gives inconsistent results whatever helper function or operator is used. For instance, both the following expression inconsistently evaluate to 1970-01-01T00:00:00Z :

      • new Date(0)
      • new Date(31536000000) // Normally should give 1971-01-01T00:00:00Z

      ISODate() helper and $toDate operator would give le same inconsistent result. This issue also leads to wrong results when performing calculations on datetime values. For instance, the following expression which is supposed to subtract one second from date 2019-01-01, wrongly results to date 2019-12-31 instead of date 2018-12-31 

      • {$subtract: [ISODate("2019-01-01"), 1000]}

       

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            faisan Faisan TIE
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: