Returns the LAST possible time unit for provided YEAR in dateTime - CSharp System

CSharp examples for System:DateTime Year

Description

Returns the LAST possible time unit for provided YEAR in dateTime

Demo Code


using System.Globalization;
using System;//ww w. ja v  a 2s.  c o m

public class Main{
        /// <summary>
        /// Returns the LAST possible time unit for provided YEAR in dateTime
        /// </summary>
        public static DateTime EndOfYear(this DateTime dateTime)
        {
            return dateTime.StartOfYear().AddYears(1).AddTicks(-1);
        }
        /// <summary>
        /// Returns the FIRST possible time unit for provided YEAR in dateTime
        /// </summary>
        public static DateTime StartOfYear(this DateTime dateTime)
        {
            var startOfYear = new DateTime(dateTime.Year, 1, 1);

            return startOfYear;
        }
}

Related Tutorials