$dateDiff (aggregation) behavior mismatch


I think there is an error in the implementation of $dateDiff aggregator operator. If I write something like this:

  $dateDiff:{startDate:{$toDate:"1976-01-18"}, endDate:"$$NOW",unit:"year"}

it return 47 and not 46.
The documentation says what I am expecting:

The $dateDiff expression returns the integer difference between the startDate and endDate measured in the specified units . Durations are measured by counting the number of times a unit boundary is passed. For example, two dates that are 18 months apart would return 1 year difference instead of 1.5 years .

Anyone else seeing this?

Thanks in advance

I think it’s correct - think of both dates truncating to January 1st of the same year and then subtract them. I get 47 years, don’t you?


Thank you Asya! But I cannot find where it says that truncates the dates before subtract. The example listed says only: Difference 18 months → 1 year difference (and not 2). This way I understood that it does the subtract and then get the duration in unit (with floor)

I think the key is the phrase “Durations are measured by counting the number of times a unit boundary is passed.” so 1.5 years is only 1 year if the year boundary (Jan 1) is passed once, if it’s passed twice then it’s 2 years. So from December 28th 2022 to today is one year (because we pass year boundary), same as January 2nd 2022 to today.


Ok but if it is really implemented this way, I would update the documentation because the example show something else. I would like to use this feature for age calculation but this way is unusable… Thanks anyway