This method returns date at the end of the month. The year used to return the start date and end date The month to return the start date and end date - CSharp System

CSharp examples for System:DateTime Year

Description

This method returns date at the end of the month. The year used to return the start date and end date The month to return the start date and end date

Demo Code


using System.IO;/*from ww  w .j av a  2 s . co  m*/
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        #endregion

            #region GetMonthEnd(int year = 0, int month = 0)
            /// <summary>
            /// This method returns date at the end of the month.
            /// <param name="year">The year used to return the start date and end date</param>
            /// <param name="month">The month to return the start date and end date</param>
            /// </summary>
            public static DateTime GetMonthEnd(int year = 0, int month = 0)
            {
                // initial value
                DateTime monthEnd;

                // locals
                int day = 0;
                int hour = 23;
                int min = 59;
                int sec = 59;

                // update the params for Year and Month if not supplied
                if (year == 0)
                {
                    // Set the value for year
                    year = DateTime.Now.Year;
                }

                // Set the value for month
                if (month == 0)
                {
                    // Set the value for month
                    month = DateTime.Now.Year;
                }

                // set the value for day
                day = DateTime.DaysInMonth(year, month);

                // now create the monthEnd date
                monthEnd = new DateTime(year, month, day, hour, min, sec);

                // return value
                return monthEnd;
            }
}

Related Tutorials