$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.
| Category | Date |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $dayOfYear: { date: <date-expression>, timezone: <timezone> } }
⚙️ Parameters
| date | Required; the date expression. |
|---|---|
| timezone | Optional 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
- Jan 1 is 1; Dec 31 is 365 in a common year, 366 in a leap year
- Use %j for a zero-padded three-digit string
- Boundaries follow UTC/timezone like $week — unify zones before stats
🏷️ Related Tags
💡 Related Cases
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.