Week numbers in C#
Support for ISO week numbers was added in .NET 5.0.
How to get the week number from a date
To get the ISO week number (1-53) from a System.DateTime instance, use System.Globalization.ISOWeek.GetWeekOfYear(date)
.
To get the corresponding four-digit year (e.g. 2024), use System.Globalization.ISOWeek.GetYear(date)
.
Read more about ISOWeek.GetWeekOfYear() and ISOWeek.GetYear() in the .NET API documentation.
How to get the date from a week number
To get the start time of a week (Monday at midnight), use System.Globalization.ISOWeek.ToDateTime(year, week, DayOfWeek.Monday))
.
year is a 4-digit year (e.g. 2024), and week is an ISO week number (1-53).
Read more about ISOWeek.ToDateTime() in the .NET API documentation.
How to get the number of weeks in a year
To get the number of ISO weeks in a year (i.e. the number of the last week), use System.Globalization.ISOWeek.GetWeeksInYear(year)
.
Read more about ISOWeek.GetWeeksInYear() in the .NET API documentation.
Read more
Learn more about week numbers and the ISO week numbering scheme in this little primer.