The $dateFromParts operator assembles a BSON Date from separate numeric fields such as year, month, and day. Use it when legacy data stores date components in different columns, or when you need to build a timestamp inside an aggregation pipeline without string parsing.
01
Build Dates
Parts → BSON Date.
02
Syntax
year, month, day.
03
Time Parts
hour, minute, second.
04
Timezone
Optional Olson zone.
05
Use Cases
Legacy data, ETL.
06
vs $dateToParts
Build vs split.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $dateFromParts operator constructs a single BSON Date from integer components. For example, if a document stores birthYear: 1995, birthMonth: 8, and birthDay: 15, $dateFromParts can combine them into one date value for sorting, filtering, or further date math with operators like $dateAdd or $dateDiff.
💡
Beginner Tip
$dateFromParts returns a Date, not a number. At minimum you must supply year, month, and day. Omitted time fields default to midnight (00:00:00.000). Requires MongoDB 5.0+.
Foundation
📝 Syntax
The $dateFromParts operator takes an object with date and optional time fields:
Each time component is optional beyond the required date parts. Use second and millisecond when you need finer precision for logging or event ordering.
Example 3 — Timezone-Aware Construction
Interpret local date/time in a specific timezone before storing as UTC:
mongosh
db.events.aggregate([
{
$project: {
title: 1,
startsAt: {
$dateFromParts: {
year: 2024,
month: 7,
day: 4,
hour: 9,
minute: 0,
timezone: "America/New_York"
}
}
}
}
])
// 9:00 AM Eastern on July 4, 2024 → stored as UTC equivalent
How It Works
Without timezone, parts are treated as UTC. With an Olson ID, MongoDB interprets the components in that zone, which matters for daylight saving and regional reporting.
Example 4 — Normalize Legacy Import Data
During ETL, replace separate date columns with a proper Date field and drop the old parts:
Use $set (or $addFields) to create the normalized date, then $unset redundant fields. After migration, index orderDate for efficient range queries.
Applications
🚀 Use Cases
Legacy databases — combine year/month/day columns from old relational imports into BSON dates.
Form submissions — merge separate dropdown values (day, month, year) into one timestamp at write time.
Reporting pipelines — construct period start/end dates from integer parameters in $facet or dashboard queries.
Inverse of $dateToParts — round-trip date decomposition and reconstruction for custom calendars.
🧠 How $dateFromParts Works
1
MongoDB evaluates each part
year, month, day, and optional time fields are resolved per document — literals or field references.
Input
2
$dateFromParts assembles the calendar
MongoDB validates the combination and applies timezone if provided.
Construct
3
A BSON Date is returned
The result is stored in the field you define — ready for sorting, indexing, or date math.
Output
=
📅
One unified timestamp
Use with $dateDiff, $dateAdd, $match on date ranges, or export to applications expecting ISO dates.
Wrap Up
Conclusion
The $dateFromParts operator is the clearest way to build BSON dates from numeric components in MongoDB aggregation pipelines. It avoids fragile string concatenation and manual parsing when your data already stores year, month, and day as separate values.
Pair it with $dateToParts when you need to split dates back into components, or $dateFromString when your source is a formatted string. Always validate that month and day values are sensible, and ensure your MongoDB deployment is version 5.0 or later.
Validate year, month, and day at insert time when possible
Use timezone when local calendar semantics matter
Index the resulting Date field after normalization
Prefer $dateFromParts over string concat + $dateFromString for numeric parts
Combine with $dateDiff once you have a proper BSON date
❌ Don’t
Use $dateFromParts on MongoDB versions before 5.0
Pass month as zero — use 1–12 (January = 1)
Assume invalid dates roll over silently — bad combos can error
Confuse $dateFromParts with $dateToParts
Forget timezone when building “local midnight” dates
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $dateFromParts
Use these points when constructing dates from components in MongoDB.
5
Core concepts
📅01
Build Dates
Parts → BSON Date.
Purpose
📝02
Three Required
year, month, day.
Syntax
🛠03
Date Output
Not a number.
Output
🌐04
Timezone
Optional Olson ID.
Option
⚠05
Not $dateToParts
Build vs split.
Important
❓ Frequently Asked Questions
$dateFromParts builds a BSON Date from separate numeric components such as year, month, and day. It is useful when dates are stored as individual fields instead of a single Date value.
{ $dateFromParts: { year: <int>, month: <int>, day: <int>, hour: <int>, minute: <int>, second: <int>, millisecond: <int>, timezone: <string> } }. year, month, and day are required; time parts default to zero.
ISODate() is a shell helper for literal date strings. $dateFromParts is an aggregation expression that constructs dates from document fields at query time inside pipelines.
Yes. Pass an optional timezone field with an Olson timezone ID such as "America/New_York" or "UTC". The date is interpreted in that zone before conversion to UTC storage.
$dateFromParts is available in MongoDB 5.0 and later in aggregation pipelines and update pipelines that use aggregation expressions.