Get the number of days in the specified month and year in CSharp

Description

The following code shows how to get the number of days in the specified month and year.

Example


using System;//from  ww w . j a va2s  . c o  m

class Example
{
  static void Main()
  {
    const int July = 7;
    const int Feb = 2;

    int daysInJuly = System.DateTime.DaysInMonth(2001, July);
        Console.WriteLine(daysInJuly);

    // daysInFeb gets 28 because the year 1998 was not a leap year. 
    int daysInFeb = System.DateTime.DaysInMonth(1998, Feb);
        Console.WriteLine(daysInFeb);

    // daysInFebLeap gets 29 because the year 1996 was a leap year. 
    int daysInFebLeap = System.DateTime.DaysInMonth(1996, Feb);
    Console.WriteLine(daysInFebLeap);
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var