ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

StartOfWeek

TakeStartOfWeek

Source

public static DateTime TakeStartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
    int diff = dt.DayOfWeek - startOfWeek;
    if (diff < 0)
    {
        diff += 7;
    }
    return dt.AddDays(-1 * diff).Date;
}

Example

DateTime weekBeginning = Week.TakeStartOfWeek(DayOfWeek.Monday);

Author: Hakan YILMAZ

Submitted on: 8 sep. 2017

Language: C#

Type: DateTime Extension

Views: 4513