Skip to main content

$dateDiff

The $dateDiff operator calculates the difference between two dates in specified units.

Input

  • An array containing two date objects and a string representing the unit of measurement (“days”, “hours”, “minutes”, “seconds”, “weeks”, “months”, “years”)

Output

  • The difference between the two dates in the specified unit

Examples

Calculate Days Difference

{
  "$dateDiff": [
    {
      "$toDate": ["2023-01-20T10:30:00Z"]
    },
    {
      "$toDate": ["2023-01-15T10:30:00Z"]
    },
    "days"
  ]
}
Result: 5

Calculate Hours Difference

{
  "$dateDiff": [
    {
      "$toDate": ["2023-01-15T15:30:00Z"]
    },
    {
      "$toDate": ["2023-01-15T10:30:00Z"]
    },
    "hours"
  ]
}
Result: 5

Calculate Field Values Difference

{
  "$dateDiff": [
    {
      "$input": "end_date"
    },
    {
      "$input": "start_date"
    },
    "months"
  ]
}
Result: The difference in months between the end_date and start_date fields