0

I need to get day of month in mongo in local timezone. Now if i run

$dayOfMonth : ISODate("2017-10-19T00:00:00.000+01:00")

i get 18, but i need that in local timezone. Is there any way how to force mongo to use local timezone with $dayOfMonth? Or at least how to get timezone offset in mongo?

  • 1
    MongoDB 3.6 does. Not now. Look at [Group by Date with Local Time Zone in MongoDB](https://stackoverflow.com/a/45093686/2313887) for all the "gory" details on timezone adjustment and daylight savings handling. To be clear about that, there is no such thing as "get the timezeone offset" because there is no timezone stored on a BSON Date. BSON Dates are in UTC. But like I said, "gory" details. Grab some popcorn and be horrified/educated. One of those. – Neil Lunn Nov 08 '17 at 08:17
  • I dont think thats exactly true, because when i store some date as ISODate in x timezone and then try to get it in y timezone it gives me the date +x timezone offset. So it needs to have the timezone stored somewhere – František Jeřábek Nov 08 '17 at 09:06
  • 1
    It actually does not store with a timezone. You are just likely viewing in something like Robo 3T which actually has an option to display "local" time to you. You probably should turn that setting off. If you want to see how things "really" are, use the plain `mongo` shell. It's in the [BSON spec](http://bsonspec.org/spec.html) if you still doubt me – Neil Lunn Nov 08 '17 at 09:08

1 Answers1

1

You can try the below aggregation in 3.6 which adds the timezone support for all date operators.

db.collection_name.aggregate({
  "dayOfMonth": {
    "$dayOfMonth": {
      "date": "$date",
      "timezone": "Europe/Amsterdam"
    }
  }
})
s7vr
  • 73,656
  • 11
  • 106
  • 127