Chinh Do

Getting the Starting Day of the Week for Any Date

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;
}
This entry was posted on Tuesday, September 7th, 2010 at 7:33 pm and is filed under Dotnet/.NET - C#, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

There is currently one response to “Getting the Starting Day of the Week for Any Date”

  1. 1 On September 12th, 2010, Weekly Link Post 161 « Rhonda Tipton's WebLog said:

    […] – Getting the Starting Day of the Week for Any Date | Lighten Color in […]

Leave a Comment