$isoWeekYear (ISO Week Year)

Last updated: 2026-10-09

$isoWeekYear (ISO Week Year)

$isoWeekYear returns the year to which a date's ISO week belongs, used with $isoWeek. Because ISO weeks can straddle years (Dec 31 may fall in week 1 of the next year), the week's year can differ from the calendar year — only $isoWeekYear gives the right ownership. Typical use: building a year-week grouping key. timezone drives the week boundary.

CategoryDate
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

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

Returns:The ISO week year; null if date is null.

▶ Example

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

// $isoWeekYear + $isoWeek: year-week grouping
 db.sales.aggregate([
  { $group: {
      _id: { y: { $isoWeekYear: { date: '$soldAt' } }, w: { $isoWeek: { date: '$soldAt' } } },
      total: { $sum: '$amount' }
  } }
])

Output:

{ "_id": { "y": 2026, "w": 39 }, "total": 9800 }
{ "_id": { "y": 2026, "w": 40 }, "total": 10450 }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat differs from $year?
A$year is the calendar year; $isoWeekYear is the week's ISO year — they can differ in straddling weeks.
QWhen do I need it?
AFor year-week grouping, combine $isoWeekYear with $isoWeek as the key.
QIs it useful alone?
ANot really — it only expresses week ownership together with $isoWeek.
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%

🙏 帮我们做得更好

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

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