The $dateDiff operator calculates how many time units lie between two dates in MongoDB aggregation pipelines. Use it to measure delivery times, ticket resolution duration, account age, or any elapsed-time metric.
01
Date Difference
Two dates → number.
02
Syntax
startDate, endDate, unit.
03
Time Units
day, hour, year, etc.
04
Timezone
Optional Olson zone.
05
Use Cases
SLA, age, shipping.
06
vs $dateAdd
Measure vs shift.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $dateDiff operator returns an integer count of complete time units between startDate and endDate. For example, if an order was placed on June 1 and delivered on June 8, $dateDiff with unit: "day" returns 7.
💡
Beginner Tip
$dateDiff returns a number, not a date. If endDate is before startDate, the result is negative. Requires MongoDB 5.0+.
Foundation
📝 Syntax
The $dateDiff operator takes an object with at least three fields:
Compute the difference in $addFields, then filter in a later $match stage. This pattern supports SLA reporting and quality dashboards.
Applications
🚀 Use Cases
Shipping and fulfillment — measure days from order to delivery for logistics KPIs.
Support SLAs — track hours or minutes tickets remain open before resolution.
User analytics — compute account age, subscription tenure, or time since last login.
Compliance reporting — verify events occurred within required time windows.
🧠 How $dateDiff Works
1
MongoDB resolves both dates
startDate and endDate are evaluated per document — fields, $$NOW, or literals.
Input
2
$dateDiff counts units
MongoDB calculates complete units between the dates, optionally using timezone.
Calculate
3
An integer is returned
The count is stored in the field you define — ready for sorting, filtering, or grouping.
Output
=
📊
Elapsed time as a number
Use in $match, $group, charts, or alongside $dateAdd for scheduling.
Wrap Up
Conclusion
The $dateDiff operator is the clearest way to measure elapsed time between two dates in MongoDB aggregation pipelines. It replaces manual millisecond subtraction and division with readable, unit-based date math.
Use $dateAdd when you need a future or past date, and $dateDiff when you need how long something took. Always ensure both operands are BSON dates and that your MongoDB deployment is version 5.0 or later.
Expect fractional units — results are truncated integers
Use $dateDiff on MongoDB versions before 5.0
Confuse $dateDiff with $dateAdd
Pass invalid unit strings (no month unit exists)
Swap startDate and endDate without expecting negative values
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $dateDiff
Use these points when measuring time between dates in MongoDB.
5
Core concepts
📊01
Measure Gap
Two dates → number.
Purpose
📝02
Three Fields
start, end, unit.
Syntax
🛠03
Integer Result
Truncated units.
Output
🌐04
Timezone
Optional Olson ID.
Option
⚠05
Not $dateAdd
Measure vs shift.
Important
❓ Frequently Asked Questions
$dateDiff returns the number of time units between two dates. It answers questions like how many days passed between an order and delivery, or how many hours a ticket stayed open.
{ $dateDiff: { startDate: <date>, endDate: <date>, unit: <string>, timezone: <string>, startOfWeek: <string> } }. startDate, endDate, and unit are required.
year, quarter, week, day, hour, minute, second, and millisecond. There is no "month" unit. Pass the unit as a string such as "day" or "hour".
$dateDiff measures the gap between two dates and returns a number. $dateAdd adds units to one date and returns a new date.
$dateDiff is available in MongoDB 5.0 and later in aggregation pipelines and update pipelines that use aggregation expressions.