$dateDiff
New from version 5.0
Not supported by Elastic cluster.
The $dateDiff aggregation operator calculates the difference between two dates in specified units. It returns the number of unit boundaries crossed between the start and end dates.
Parameters
-
startDate: The beginning date expression. -
endDate: The ending date expression. -
unit: The time unit for the difference. Supported units areyear,quarter,month,week,day,hour,minute,second, andmillisecond.
Example (MongoDB Shell)
The following example demonstrates how to use the $dateDiff operator to calculate the number of days between order placement and delivery.
Create sample documents
db.shipments.insertMany([ { orderId: 1001, orderDate: ISODate("2025-01-10T08:00:00Z"), deliveryDate: ISODate("2025-01-15T14:30:00Z") }, { orderId: 1002, orderDate: ISODate("2025-02-05T10:00:00Z"), deliveryDate: ISODate("2025-02-12T16:45:00Z") } ]);
Query example
db.shipments.aggregate([ { $project: { orderId: 1, orderDate: 1, deliveryDate: 1, daysToDeliver: { $dateDiff: { startDate: "$orderDate", endDate: "$deliveryDate", unit: "day" } } } } ]);
Output
[
{
_id: ObjectId('6924a5f2d66dcae121d29517'),
orderId: 1001,
orderDate: ISODate('2025-01-10T08:00:00.000Z'),
deliveryDate: ISODate('2025-01-15T14:30:00.000Z'),
daysToDeliver: 5
},
{
_id: ObjectId('6924a5f2d66dcae121d29518'),
orderId: 1002,
orderDate: ISODate('2025-02-05T10:00:00.000Z'),
deliveryDate: ISODate('2025-02-12T16:45:00.000Z'),
daysToDeliver: 7
}
]
Code examples
To view a code example for using the $dateDiff command, choose the tab for the language that you want to use: