DateTime - EndOfDay
A DateTime extension method that return a DateTime with the time set to "23:59:59:999". The last moment of the day. Use "DateTime2" column type in sql to keep the precision.
public static void Main() { DateTime date = DateTime.Now; Console.WriteLine("Original DateTime : {0}", date.ToFullDateTimeString()); // C# Extension Method: DateTime - EndOfDay DateTime endOfDay = date.EndOfDay(); Console.WriteLine("DateTime after calling EndOfDay: {0}", endOfDay.ToFullDateTimeString()); }
View Source
using System; public static partial class Extensions { /// <summary> /// A DateTime extension method that return a DateTime with the time set to "23:59:59:999". The last moment of /// the day. Use "DateTime2" column type in sql to keep the precision. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>A DateTime of the day with the time set to "23:59:59:999".</returns> public static DateTime EndOfDay(this DateTime @this) { return new DateTime(@this.Year, @this.Month, @this.Day).AddDays(1).Subtract(new TimeSpan(0, 0, 0, 0, 1)); } }