Get Week Commencing Date from DateTime in C#

This code will calculate the week commencing date for a given DateTime and return a DateTime containing the date of the start of the week.

By Tim Trott | C# ASP.Net MVC | March 4, 2010

The first day of the week is governed by the system CultureInfo. For some regions, the first day of the week is Monday, whilst for others it is Sunday. For example, in the UK (en-GB) culture, the first day will be a Monday, whereas in America (en-US) culture it will be a Sunday.

C#
public static DateTime GetWeekCommencing(DateTime date)
{
  CultureInfo info = Thread.CurrentThread.CurrentCulture;
  DayOfWeek firstday = info.DateTimeFormat.FirstDayOfWeek;
  DayOfWeek today = info.Calendar.GetDayOfWeek(date);

  int diff = today - firstday;
  DateTime firstDate = date.AddDays(-diff);
  return firstDate;
}
Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

We respect your privacy, and will not make your email public. Learn how your comment data is processed.