High-Performance Development - C#, VB, SQL, ASP.NET

Slogan Text

This article describes how to determine the number of days in a month using the ASP.NET DateTime class.

  1. Create new DateTime object with current year and month, and 1st day of month.
  2. Step forward one month. We're now at the 1st day of the next month.
  3. Step backwards one day. We're now at the last day of the input month.
  4. 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;