Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DateTime.DaysInMonth Method

Returns the number of days in the specified month of the specified year.

[Visual Basic]
Public Shared Function DaysInMonth( _
   ByVal year As Integer, _
   ByVal month As Integer _
) As Integer
[C#]
public static int DaysInMonth(
 int year,
 int month
);
[C++]
public: static int DaysInMonth(
 int year,
 int month
);
[JScript]
public static function DaysInMonth(
   year : int,
 month : int
) : int;

Parameters

year
The year.
month
The month (a number ranging from 1 to 12).

Return Value

The number of days in month for the specified year.

For example, if month equals 2 for February, the return value is 28 or 29 depending upon whether year is a leap year.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException month is less than 1 or greater than 12.

Example

[Visual Basic, C#, C++] The following sample demonstrates the DaysInMonth method.

[Visual Basic] 
Const July As Integer = 7
Const Feb As Integer = 2

' daysInJuly gets 31.
Dim daysInJuly As Integer = System.DateTime.DaysInMonth(2001, July)

' daysInFeb gets 28 because the year 1998 was not a leap year.
Dim daysInFeb As Integer = System.DateTime.DaysInMonth(1998, Feb)

' daysInFebLeap gets 29 because the year 1996 was a leap year.
Dim daysInFebLeap As Integer = System.DateTime.DaysInMonth(1996, Feb)

[C#] 
const int July = 7;
const int Feb = 2;

// daysInJuly gets 31.
int daysInJuly = System.DateTime.DaysInMonth(2001, July);

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

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

[C++] 
const int July = 7;
const int Feb = 2;

// daysInJuly gets 31.
int daysInJuly = System::DateTime::DaysInMonth(2001, July);

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

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

DateTime Structure | DateTime Members | System Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft