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

CSharp examples for System:DateTime Month

Description

Returns the LAST possible time unit for provided MONTH in dateTime

Demo Code


using System.Globalization;
using System;/*from  ww  w .  j av  a2  s.  c  om*/

public class Main{
        /// <summary>
        /// Returns the LAST possible time unit for provided MONTH in dateTime
        /// </summary>
        public static DateTime EndOfMonth(this DateTime dateTime)
        {
            return dateTime.StartOfMonth().AddMonths(1).AddTicks(-1);
        }
        /// <summary>
        /// Returns the FIRST possible time unit for provided MONTH in dateTime
        /// </summary>
        public static DateTime StartOfMonth(this DateTime dateTime)
        {
            DateTime startOfMonth = dateTime.AddDays(-dateTime.Day + 1).Date;

            return startOfMonth;
        }
}

Related Tutorials