2 / 4
Apr 2024

I am confused about how $dateTrunc works. For example, consider the following:

`
ISODate(“2024-04-24T01:00:00Z”)

$dateTrunc: { date: "$date", unit: "hour", binSize: 2, timezone: "+02:00", startOfWeek: "Monday" }

`

In this case, I get the result: 2024-04-24T00:00:00Z, which is correct. However, when I use the same input and corresponding timezone:

$dateTrunc: { date: "$date", unit: "hour", binSize: 2, timezone: "Europe/Warsaw", startOfWeek: "Monday" }

I get 2024-04-24T01:00:00Z (where I should get the same result).

What is happening?

Hi @J_D,

The Timezone Europe/Warsaw considers UTC+01:00 offset as per the link,

You can also refer to this $dateTrunc operator documentation has mentioned about timezone,

The timezone for the $dateTrunc calculation, specified as an expression that must resolve to a string that contains one of these values:

If no timezone is provided, the $dateTrunc calculation is performed in UTC.

Thanks for your response. While Europe/Warsaw does use UTC+01:00 during standard time, it switches to UTC+02:00 during daylight saving time (DST). Since the example date I provided (2024-04-24T01:00:00Z) falls within the DST period, MongoDB should correctly apply the UTC+02:00 offset when using the “Europe/Warsaw” timezone name, giving the same result as using the explicit “+02:00” offset.

There is a small note for $dateSubstract:

When using an Olson Timezone Identifier in the field, MongoDB applies the DST offset if applicable for the specified timezone.

Not sure if this also applies to $dateTrunc