Have you ever asked yourself how to calculate the date of the first day in a week, given the week number and a year in C#? The Gregorian Calendar has fifty-two full weeks and an extra day in a normal year, equivalent to 52.143. However, it also includes leap years with fifty-two full weeks and two extra days, equivalent to 52.286 weeks.
In this article, we will learn how to accomplish our goal of finding the date of the first day based on a specified week number.
Without further delay, let’s start!
How to Calculate the First Day of the Week Given the Week Number in C#
There are six calendar systems: Gregorian, Jewish, Islamic, Indian, Chinese, and Julian Calendars. Most countries use the Gregorian Calendar, which forms the foundation for the ISO 8601 standard. This standard recognizes Monday as the first day of the week and Sunday as the last day of the week. In this article, we will implement examples that comply with the ISO 8601 standard:
private static readonly Calendar _gregorianCalendar = new GregorianCalendar();
Sometimes, January 1st may not fall on a Monday. Therefore, we need to be careful when calculating the first day of the week when we know the week number and year. In our example, we will select the first Thursday of the year as week 1. We do this mainly to ensure that the first week always contains at least four days, making our calculations consistent. For example, if January 1 falls on a Wednesday, the first Thursday still falls in the same week and part of the first week, which ensures that the week starts with a reasonable number of days.
Example of How to Get the First Day of the Week in C#
Let’s implement a method that accepts the year and week number as parameters to help us accomplish our objective:
public static DateTime FirstDateOfWeekISO8601(int year, int weekNumber) { var firstOfJan = new DateTime(year, 1, 1); var daysOffset = DayOfWeek.Thursday - firstOfJan.DayOfWeek; var firstThursdayOfYear = firstOfJan.AddDays(daysOffset); var firstWeekOfYear = _gregorianCalendar.GetWeekOfYear(firstThursdayOfYear, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); if (firstWeekOfYear == 1) { weekNumber -= 1; } return firstThursdayOfYear.AddDays(weekNumber * 7 - 3); }
First, we create a DateTime
instance for the first day of the specified year
, which we’ll use as our reference point. Next, we compute the difference in days of the week between Thursday and the day of the week for January 1st (which may be negative).
Once we calculate the difference, daysOffset
, we work our way to determine the first Thursday of the year by adding the days to our firstOfJan
date. Next, using our already initialized Gregorian calendar object we extract the week of the year by invoking the GetWeekOfYear()
method. We pass the firstThursdayOfYear
as the DateTime
which must be included in the week. Next, we select the FirstFourDayWeek
calendar week rule while also setting Monday as the first day, complying with the ISO 8601 standard.
Next, we check if the weekNumber
is one and decrease it by one to ensure we compute the correct dates for the first week of the year.
Lastly, we multiply weekNumber
by seven to calculate the number of days from the first Thursday to the specified week’s Thursday. But since our goal is to find the first Monday, we need to subtract 3 from this total. With that calculation done, we can compute a new DateTime object by adding this total to firstThursdayOfYear
.
For example, when we pass the year 2020 and week one as parameters to our method:
var result = FirstDayOfWeekMethods.FirstDateOfWeekISO8601(2020, 1); Console.WriteLine(result);
We see the expected result of 30th December, 2019 since January 1st falls on a Wednesday and Monday falls in the previous year:
12/30/2019 12:00:00 AM
Conclusion
In this article, we learn how to get the first day in a week when we have the week number and year in C#. This skill can prove invaluable, especially when building reporting and scheduling applications requiring accurate date calculations. Using our example, we can extend the method to work with other calendars and standards if necessary. Which technique have you used before? Let us know in the comments section below.