$dateAdd (Add to a Date)

Last updated: 2026-10-09

$dateAdd (Add to a Date)

$dateAdd adds a specified amount of a time unit to a date and returns the new date — the go-to operator for relative dates in pipelines (due dates, renewal dates, deadlines). unit supports year, month, week, day, hour, minute, second, and millisecond; amount may be negative, equivalent to subtracting. With timezone you can compute against a specific time zone's calendar for accurate month/year boundaries. Works in $project, $addFields, $set and nested expressions.

CategoryDate
MongoDB Version5.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $dateAdd: { startDate: <date-expression>, unit: <unit>, amount: <number>, timezone: <timezone> } }

⚙️ Parameters

startDateThe date to modify; a Date, timestamp, or date-resolving expression.
unitThe unit: year, month, week, day, hour, minute, second, or millisecond.
amountThe amount to add; positive adds, negative subtracts; out of 64-bit integer range throws.
timezoneOptional timezone for the calculation; IANA name (e.g. Asia/Shanghai) or UTC offset.

Returns:The new date (BSON Date); null if startDate is null or missing.

▶ Example

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

// $dateAdd: due date is 30 days after creation (Beijing time)
db.orders.aggregate([
  { $project: {
      dueDate: { $dateAdd: { startDate: '$createdAt', unit: 'day', amount: 30, timezone: 'Asia/Shanghai' } }
  } }
])

Output:

{ "_id": 1, "dueDate": ISODate("2026-10-25T00:00:00Z") }
{ "_id": 2, "dueDate": ISODate("2026-11-15T00:00:00Z") }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QCan $dateAdd add several units at once?
AOne unit per call; nest calls or chain multiple $dateAdd for several units.
QDoes amount support negative values?
AYes — negative amounts subtract, or use $dateSubtract instead.
QHow do I specify the timezone?
AUse an IANA name like Asia/Shanghai or an offset like +08:00; defaults to UTC.
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%

🙏 帮我们做得更好

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

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