$dayOfYear (Get Day of Year)

Last updated: 2026-10-09

$dayOfYear (Get Day of Year)

$dayOfYear returns the day of the year of a date (1 to 366) — useful for yearly progress, seasonal analysis, or days-remaining calculations. Counting starts at 1; a common year tops out at 365, a leap year at 366. It computes in UTC or the given timezone, and the boundary shifts across zones, so pass timezone explicitly. Pair with the %j placeholder of $dateToString for a zero-padded three-digit string.

CategoryDate
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $dayOfYear: { date: <date-expression>, timezone: <timezone> } }

⚙️ Parameters

dateRequired; the date expression.
timezoneOptional timezone; defaults to UTC.

Returns:Day of year (1-366) as an integer; null if date is null.

▶ Example

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

// $dayOfYear: day-of-year of events
 db.events.aggregate([
  { $project: {
      doy: { $dayOfYear: '$eventDate' },
      label: { $dateToString: { format: '%j', date: '$eventDate' } }
  } }
])

Output:

{ "_id": 1, "doy": 268, "label": "268" }
{ "_id": 2, "doy": 1, "label": "001" }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat is the yearly maximum?
A365 in a common year, 366 in a leap year.
QWhat differs from $dayOfMonth?
A$dayOfMonth is the day within the month; $dayOfYear is the day within the year.
QHow to get a three-digit string?
AUse the %j placeholder of $dateToString, which zero-pads.
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%

🙏 帮我们做得更好

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

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