Learning never exhausts the mind

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;
}

 

Did you enjoy this post? Let others know!

If you enjoyed this post, please share it with others. Click one of the social media buttons below to share on Facebook, Twitter, LinkedIn, Pinterest or email to a friend.

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.