DateTime - StartOfYear
A DateTime extension method that return a DateTime of the first day of the year with the time set to "00:00:00:000". The first moment of the first day of the year.
public static void Main() { DateTime todayDate = DateTime.Now; Console.WriteLine("Current Date : {0}", todayDate.ToFullDateTimeString()); // C# Extension Method: DateTime - StartOfYear DateTime newDate = todayDate.StartOfYear(); Console.WriteLine("After calling StartOfYear: {0}", newDate.ToFullDateTimeString()); }
View Source
using System; public static partial class Extensions { /// <summary> /// A DateTime extension method that return a DateTime of the first day of the year with the time set to /// "00:00:00:000". The first moment of the first day of the year. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>A DateTime of the first day of the year with the time set to "00:00:00:000".</returns> public static DateTime StartOfYear(this DateTime @this) { return new DateTime(@this.Year, 1, 1); } }