How to get the DateTime for the start of the week in C#:
In this post, we will learn how to get the DateTime for the start of the week. We will write one method, that will take one DateTime and return one DateTime for the start day of the week for the given datetime.
How to do that:
This is actually easy if you are not supporting different timezone. For example, the starting date of a week might be different in different timezones. The starting day of a week might be Sunday in one timezone and Monday in another timezone.
We can get the first day of a week for the current timezone or culture.
The easiest and safest way to solve this is by keep subtracting 1 from the given day till it becomes equal to the week start day.
C# example program:
The below program illustrates how it works:
using System;
using System.Threading;
public class Program
{
public static void Main()
{
var givenDate = DateTime.Today;
var firstDayOfWeek = Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
Console.WriteLine("Given date: "+givenDate.DayOfWeek.ToString() + "," + givenDate.ToString());
Console.WriteLine("First Week day: "+firstDayOfWeek.ToString());
while (givenDate.DayOfWeek != firstDayOfWeek)
givenDate = givenDate.AddDays(-1);
Console.WriteLine("Modified date: "+givenDate);
}
}
In this program,
- givenDate is the date-time given. We are using today for this example, but it can be any DateTime.
- firstDayOfWeek gives the first day of the week from the current culture.
- The first WriteLine is printing the given date.
- The second WriteLine is printing the first day of the week.
- The while loop adds -1 to the given date. It returns one day before the day. This loop iterates till the DayOfWeek becomes equal to the firstDayOfWeek. i.e. it keeps subtracting one day until the given date becomes the first day of the week.
- The last WriteLine is printing the modified DateTime, i.e. DateTime for the first day of the week.
If you run this program, it will give output as like below:
Given date: Monday,8/30/2021 12:00:00 AM
First Week day: Sunday
Modified date: 8/29/2021 12:00:00 AM
It will always give the first-week day irrespective of the current culture.
Example with a different culture:
We can change the current application culture by changing the Thread.CurrentThread.CurrentUICulture property. In the below program, we are changing this to Latvian, where Monday is the starting day of the week.
using System;
using System.Threading;
using System.Globalization;
public class Program
{
public static void Main()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("lv-LV");
var givenDate = DateTime.Today;
var firstDayOfWeek = Thread.CurrentThread.CurrentUICulture.DateTimeFormat.FirstDayOfWeek;
Console.WriteLine("Given date: "+givenDate.DayOfWeek.ToString() + "," + givenDate.ToString());
Console.WriteLine("First Week day: "+firstDayOfWeek.ToString());
while (givenDate.DayOfWeek != firstDayOfWeek)
givenDate = givenDate.AddDays(-1);
Console.WriteLine("Modified date: "+givenDate);
}
}
It will print:
Given date: Monday,8/30/2021 12:00:00 AM
First Week day: Monday
Modified date: 8/30/2021 12:00:00 AM
Since the given day is Monday, it will not change it. Note that we are reading CurrentUICulture in this example.