Calculate Calendar Week (Part 2)

by Feb 11, 2022

In the previous tip we explained how to calculate the calendar week for a given date. As you have seen, this depends on culture and calendar settings and can be different across cultures.

This is why there is also the “ISOWeek”: it adheres to ISO 8601 and is standardized. Unfortunately, the classic APIs in .NET do not always calculate the correct ISOWeek.

That’s why Microsoft added a whole new class called “ISOWeek” to .NET Standard (the portable .NET that PowerShell 7 uses) and .NET Framework 5.

The following line returns the ISOWeek for any date (when run in PowerShell 7):

 
PS> [System.Globalization.ISOWeek]::GetWeekOfYear('2022-01-01') 
52 
 

The same line returns a red exception when run in Windows PowerShell since Windows PowerShell is based on the full .NET Framework which in current versions does not yet support this API.


Twitter This Tip! ReTweet this Tip!