Returns first and last dates of the month specified in date param - CSharp System

CSharp examples for System:DateTime Month

Description

Returns first and last dates of the month specified in date param

Demo Code


using System.Threading;
using System.Globalization;
using System.Collections.Generic;
using System;/*  w  ww  .jav a 2s  . c o m*/

public class Main{
        /// <summary>
        /// Returns first and last dates of the month specified in date param
        /// </summary>
        /// <param name="date">Date to get month from</param>
        /// <returns>Pair of first and last days of month</returns>
        public static Tuple<DateTime, DateTime> GetMonthStartAndEnd(this DateTime date)
        {
            return new Tuple<DateTime, DateTime>(date.GetFirstDay(), date.GetLastDay());
        }
        /// <summary>
        /// Method for getting last day in month
        /// </summary>
        /// <param name="date">Date</param>
        /// <returns>Last day in month</returns>
        public static DateTime GetLastDay(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
        }
        /// <summary>
        /// Method for getting first day in month
        /// </summary>
        /// <param name="date">Date</param>
        /// <returns>First day in month</returns>
        public static DateTime GetFirstDay(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, 1);
        }
}

Related Tutorials