Learning never exhausts the mind

Published on by

This snippet will calculate the date of the start of the week from a given DateTime and return a DateTime containing the week commencing date.
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;
}

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.