Cheat Sheet: Date and Time Operators
Date extraction, arithmetic, truncation, parsing, formatting, and system variables.
$year / $month / $dayOfMonth
Reference group: Date expressions
What it does
Extracts calendar components; timezone affects local calendar boundaries.
Syntax
Date-part expressions with optional timezone
Use case
Group/display by local date.
{ $project: { year: { $year: { date: "$createdAt", timezone: "Asia/Kolkata" } }, month: { $month: "$createdAt" } } }- Extracts calendar components; timezone affects local calendar boundaries.
- Typical use: Group/display by local date.
$hour / $minute / $second / $millisecond
Reference group: Date expressions
What it does
Extracts time components; without timezone, date-part operators use UTC.
Syntax
Date-part expressions
Use case
Analyze activity by hour.
{ $project: { hourUtc: { $hour: "$createdAt" } } }- Extracts time components; without timezone, date-part operators use UTC.
- Typical use: Analyze activity by hour.
$dayOfWeek / $dayOfYear / $isoWeek / $isoWeekYear
Reference group: Date expressions
What it does
Extracts weekday, day-of-year, or ISO week data.
Syntax
Date-part expressions
Use case
Weekly reports.
Important notes
ISO week-year may differ from calendar year near New Year.
{ $project: { week: { $isoWeek: "$createdAt" }, isoYear: { $isoWeekYear: "$createdAt" } } }- Extracts weekday, day-of-year, or ISO week data.
- Typical use: Weekly reports.
$dateAdd
Reference group: Date expressions
What it does
Adds a date/time unit.
Syntax
{$dateAdd: { startDate, unit, amount, timezone? }}
Use case
Due dates and expiration times.
{ $set: { dueAt: { $dateAdd: { startDate: "$createdAt", unit: "day", amount: 7 } } } }- Adds a date/time unit.
- Typical use: Due dates and expiration times.
$dateSubtract
Reference group: Date expressions
What it does
Subtracts a date/time unit.
Syntax
{$dateSubtract: { startDate, unit, amount, timezone? }}
Use case
Rolling windows.
{ $set: { windowStart: { $dateSubtract: { startDate: "$$NOW", unit: "day", amount: 30 } } } }- Subtracts a date/time unit.
- Typical use: Rolling windows.
$dateDiff
Reference group: Date expressions
What it does
Counts unit boundaries crossed, not simply elapsed milliseconds divided by a constant.
Syntax
{$dateDiff: { startDate, endDate, unit, timezone?, startOfWeek? }}
Use case
Calendar duration calculations.
{ $project: { daysOpen: { $dateDiff: { startDate: "$openedAt", endDate: "$$NOW", unit: "day" } } } }- Counts unit boundaries crossed, not simply elapsed milliseconds divided by a constant.
- Typical use: Calendar duration calculations.
$dateTrunc
Reference group: Date expressions
What it does
Truncates dates to a period boundary.
Syntax
{$dateTrunc: { date, unit, binSize?, timezone?, startOfWeek? }}
Use case
Group by day/week/month.
{ $group: { _id: { $dateTrunc: { date: "$createdAt", unit: "week", timezone: "Asia/Kolkata" } }, count: { $sum: 1 } } }- Truncates dates to a period boundary.
- Typical use: Group by day/week/month.
$dateToString
Reference group: Date expressions
What it does
Formats a BSON Date as a string.
Syntax
{$dateToString: { date, format?, timezone?, onNull? }}
Use case
Report labels.
Important notes
Keep stored dates as BSON Date for filtering/sorting.
{ $project: { localDate: { $dateToString: { date: "$createdAt", format: "%Y-%m-%d %H:%M", timezone: "Asia/Kolkata" } } } }- Formats a BSON Date as a string.
- Typical use: Report labels.
$dateFromString
Reference group: Date expressions
What it does
Parses date text into BSON Date.
Syntax
{$dateFromString: { dateString, format?, timezone?, onError?, onNull? }}
Use case
Normalize imported dates.
{ $set: { parsed: { $dateFromString: { dateString: "$dateText", format: "%Y-%m-%d", onError: null } } } }- Parses date text into BSON Date.
- Typical use: Normalize imported dates.
$dateFromParts / $dateToParts
Reference group: Date expressions
What it does
Constructs dates from parts or decomposes dates.
Syntax
Date construction/decomposition expressions
Use case
Build period boundaries or extract multiple components.
{ $project: { parts: { $dateToParts: { date: "$createdAt", timezone: "Asia/Kolkata" } } } }- Constructs dates from parts or decomposes dates.
- Typical use: Build period boundaries or extract multiple components.