DateTime - LastDayOfWeek
A DateTime extension method that return a DateTime of the last day of week.
public static void Main() { DateTime now = DateTime.Now; Console.WriteLine("Current Date : {0}", now.ToFullDateTimeString()); // C# Extension Method: DateTime - LastDayOfWeek DateTime lastDayOfWeek = now.LastDayOfWeek(); Console.WriteLine("Last day of week: {0}", lastDayOfWeek.ToFullDateTimeString()); }
View Source
using System; public static partial class Extensions { /// <summary> /// A DateTime extension method that last day of week. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>A DateTime.</returns> public static DateTime LastDayOfWeek(this DateTime @this) { return new DateTime(@this.Year, @this.Month, @this.Day).AddDays(6 - (int) @this.DayOfWeek); } }