$dateToParts (Split Date into Parts)

Last updated: 2026-10-09

$dateToParts (Split Date into Parts)

$dateToParts splits a date into a document of year, month, day, hour, minute, second, and millisecond — the inverse of $dateFromParts. Set iso8601 to true to instead get isoWeekYear, isoWeek, and isoDayOfWeek. Common uses: group stats by year/month (as $group _id) or export dates as readable documents. timezone drives the split and defaults to UTC — always set it for cross-zone reporting.

CategoryDate
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $dateToParts: { date: <date-expression>, timezone: <timezone>, iso8601: <boolean> } }

⚙️ Parameters

dateRequired; the date expression to split.
timezoneOptional timezone; defaults to UTC.
iso8601Optional; true outputs ISO week-calendar fields.

Returns:A document with year/month/day/hour/minute/second/millisecond (or ISO fields); null if date is null.

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $dateToParts: split order time into parts (Beijing time)
db.orders.aggregate([
  { $project: { parts: { $dateToParts: { date: '$createdAt', timezone: 'Asia/Shanghai' } } } }
])

Output:

{ "_id": 1, "parts": { "year": 2026, "month": 9, "day": 25, "hour": 8, "minute": 30, "second": 0, "millisecond": 0 } }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat are the output field names?
AFixed keys: year, month, day, hour, minute, second, millisecond.
QHow to group stats by month?
ASplit with $dateToParts, then $group with _id as a year/month combination.
QWhat does iso8601 do?
AWhen true, outputs ISO week-calendar fields for week-based stats.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏