$dateFromParts is a MongoDB aggregation expression that builds a Date from separate numbers: year, month, day, and optional hour/minute/second. Use it when a document stores date pieces as fields instead of one Date.
Set at least year (add month and day for a full calendar date).
Optionally set time parts and timezone.
Use the resulting Date with filters, sorts, or other date operators.
Piece
Required?
Role
year
Yes
Calendar year
month / day
No
Default to 1 if omitted
Time parts
No
Default to 0
timezone
No
Interpret parts in a named zone
Pseudocode
Pseudocode
for each document:
y = evaluate(year)
m = evaluate(month) or 1
d = evaluate(day) or 1
h, mi, s, ms = time parts or 0
if year is null or missing:
result = null
else:
result = Date(y, m, d, h, mi, s, ms) // + timezone if set
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while building dates from parts.
Topic
Detail
Type
Aggregation expression (not a find filter)
Minimum
{ year: <n> }
Defaults
month/day → 1; time → 0
Inverse
$dateToParts
String input
Use $dateFromString instead
ISO weeks
Also supports isoWeekYear / isoWeek / isoDayOfWeek
Sample check
2026, 1, 10 → 2026-01-10T00:00:00Z
Try it
Live Preview
Watch $dateFromParts turn year/month/day (and time) into one Date.
Simulates { $dateFromParts: { year, month, day, ... } } in UTC.
Result
year = 2026, month = 1, day = 10
$dateFromParts → 2026-01-10T00:00:00.000Z
Trace
Worked Walkthrough — Event Date
An event stores year 2026, month 1, and day 10 as separate numbers. We want one Date field.
Calculation
Document:
{ year: 2026, month: 1, day: 10 }
$dateFromParts:
year = 2026
month = 1
day = 10
(time defaults to 00:00:00.000)
Result → 2026-01-10T00:00:00.000Z ✓
Code
MongoDB Examples
We use one event with numeric date parts. First build a date-only value, then include hour and minute. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one launch event stored as separate numbers: year 2026, month 1, day 10, hour 9, minute 30. We save them in a collection named events.
Parts are interpreted in the given zone before storage as UTC. Set timezone when local wall time matters.
Analysis
Performance Notes
Approach
Cost
Best for
$dateFromParts per document
Cheap construction
Legacy schemas with split date fields
Build then $match
Compute on scanned docs
Filter after assembling a Date
Store a real Date on write
No rebuild on read
Hot queries that need Date indexes
Building a Date is cheap. Filter early with $match when only some documents need assembly.
Remember
Key Takeaways
Role: numbers → one Date.
Required:year; month/day default to 1.
Time: hour/minute/second default to 0.
Inverse:$dateToParts; strings → $dateFromString.
One line:$dateFromParts assembles a Date from year/month/day (and optional time) fields.
Frequently Asked Questions
$dateFromParts builds a BSON Date from separate numeric parts such as year, month, and day. Use it inside aggregation stages when dates are stored as individual fields.
Use { $dateFromParts: { year: <n>, month: <n>, day: <n>, hour: <n>, minute: <n>, second: <n>, millisecond: <n>, timezone: <tz> } }. year is required; month and day default to 1; time parts default to 0.
$dateFromParts builds a date from numeric fields. $dateFromString parses a date string with a format pattern.
$dateToParts splits a Date into year, month, day, and other parts.
Yes. Pass timezone with an Olson ID such as "America/New_York" or "UTC" when the parts should be interpreted in that zone.
MongoDB normalizes overflowing parts (for example month 13 becomes the next year). Prefer valid ranges so results stay easy to reason about.