DateTime - EndOfYear

A DateTime extension method that return a DateTime of the last day of the year with the time set to "23:59:59:999". The last moment of the last day of the year. Use "DateTime2" column type in sql to keep the precision.

Try it

public static void Main()
{
    DateTime date = DateTime.Now;
	
	Console.WriteLine("Original DateTime               : {0}", date.ToFullDateTimeString());

    // C# Extension Method: DateTime - EndOfYear
	DateTime endOfDay = date.EndOfYear();
	
	Console.WriteLine("DateTime after calling EndOfYear: {0}", endOfDay.ToFullDateTimeString());
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     A DateTime extension method that return a DateTime of the last day of the year with the time set to
    ///     "23:59:59:999". The last moment of the last day of the year.  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 last day of the year with the time set to "23:59:59:999".</returns>
    public static DateTime EndOfYear(this DateTime @this)
    {
        return new DateTime(@this.Year, 1, 1).AddYears(1).Subtract(new TimeSpan(0, 0, 0, 0, 1));
    }
}