Liefert zu einem Datum den ersten Tag der Woche zurück in der das Datum liegt
/// <summary>
/// Resolves the first day of the week of the given DateTime value.
/// </summary>
/// <param name="source">The given DateTimeValue.</param>
/// <returns>The DateTime.Date of the first day of the week of the given DateTime value.</returns>
/// <remarks>If the given DateTime is the first day of the week, then the source.Date will be the result.</remarks>
public static DateTime FirstDayOfWeek(this DateTime source)
{
DayOfWeek day = source.DayOfWeek;
DayOfWeek weekBegin = System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;
DateTime result;
if (day >= weekBegin)
{
int offSet = day - weekBegin;
result = source.AddDays(offSet * -1).Date;
}
else
{
result = source.AddDays(((int)day)-6);
}
return result;
}
1 Kommentare zum Snippet