Chinh Do

Getting the Starting Day of the Week for Any Date

First time here? Check out my greatest hits or look around in the archives, and consider subscribing to the latest posts via RSS or email. I am also on Twitter and Google Plus. Thanks for visiting.
7th September 2010

Getting the Starting Day of the Week for Any Date

From my code snippets, here’s a function that will return the starting day of the week for any date:

/// <summary>
/// Gets the start of the week that contains the specified date.
/// </summary>
/// <param name="date">The date.</param>
/// <param name="weekStartsOn">The day that each week starts on.</param>
/// <returns>The start date of the week.</returns>
public static DateTime GetStartOfWeek(DateTime date, DayOfWeek weekStartsOn)
    int days = date.DayOfWeek - weekStartsOn;
    DateTime startOfWeek = days>=0 ? date.AddDays(-days) : date.AddDays(-7 - days);
    return startOfWeek;

posted in Dotnet/.NET - C#, Programming | 1 Comment

  • Calendar

  • September 2010
    M T W T F S S
    « Jun   Jan »