How to Get Number of Days in Months
This article describes how to determine the number of days in a month
using the ASP.NET DateTime class.
- Create new DateTime object with current year and month, and 1st day of month.
- Step forward one month. We're now at the 1st day of the next month.
- Step backwards one day. We're now at the last day of the input month.
- Get and return the Day number from the DateTime object.
public int DaysInMonth(DateTime DateObj)
{
DateTime Date2Obj;
int Out;
Date2Obj = new DateTime(DateObj.Year, DateObj.Month, 1);
Date2Obj = Date2Obj.AddMonths(1);
Date2Obj = Date2Obj.AddDays(-1);
Out = Date2Obj.Day;
return Out;
}
|