In C#, DateTime.DaysInMonth(year, month) returns the number of days in that month. You need the year because February has 28 or 29 days.
It is the safe way to find the last day of a month: pass the result as the day argument of a new DateTime.
This is a static member of System.DateTime. You call it with the type name, such as DateTime.DaysInMonth(...). DateTime holds a date and time of day with a Kind of Utc, Local, or Unspecified.
Syntax
Call it with the type name and pass a year and a month number (1 to 12).
C#
int nov = DateTime.DaysInMonth(2026, 11); // 30
int dec = DateTime.DaysInMonth(2026, 12); // 31
int feb = DateTime.DaysInMonth(2028, 2); // 29 (leap year)
Call
What it does
DateTime.DaysInMonth(2026, 11)
November always has 30 days.
DateTime.DaysInMonth(2026, 2)
February in a normal year: 28.
DateTime.DaysInMonth(2028, 2)
February in a leap year: 29.
The month must be 1 to 12 and the year must be 1 to 9999. Anything else throws ArgumentOutOfRangeException.
Library declarations
C#
public static int DaysInMonth(int year, int month);
Parameters
Parameter
Description
year
Required. A year from 1 to 9999.
month
Required. A month number from 1 (January) to 12 (December).
Return Value
Return Value
Description
int
The number of days in that month: 28, 29, 30, or 31.
C# Examples
Example 1 — Days in Three Months
Read the day count for November, December, and February.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(DateTime.DaysInMonth(2026, 11));
Console.WriteLine(DateTime.DaysInMonth(2026, 12));
Console.WriteLine(DateTime.DaysInMonth(2026, 2));
}
}
Output
30
31
28
How It Works
November. It is one of the four months with 30 days.
December. It has 31 days like January, March, May, July, August, and October.
February 2026. The year is not a leap year, so the answer is 28.
No date needed. You pass numbers only; no DateTime object is created.
Example 2 — February in Different Years
Compare leap years, a normal year, and a century year.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(DateTime.DaysInMonth(2028, 2));
Console.WriteLine(DateTime.DaysInMonth(2100, 2));
Console.WriteLine(DateTime.DaysInMonth(2000, 2));
}
}
Output
29
28
29
How It Works
2028. It is divisible by 4 and not a century, so February has 29 days.
2100. Century years are leap only when divisible by 400, so this February has 28 days.
2000. It is divisible by 400, so it is a leap year with 29 days.
Rule built in. You do not code the leap-year rule yourself.
Example 3 — Last Day, Year Total, Bad Month
Build the last day of November, add up a year, and catch an invalid month.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
int last = DateTime.DaysInMonth(2026, 11);
DateTime end = new DateTime(2026, 11, last);
Console.WriteLine(end.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
int total = 0;
for (int m = 1; m <= 12; m++) total += DateTime.DaysInMonth(2026, m);
Console.WriteLine(total);
try {
DateTime.DaysInMonth(2026, 13);
} catch (ArgumentOutOfRangeException) {
Console.WriteLine("Month must be 1-12");
}
}
}
Output
2026-11-30
365
Month must be 1-12
How It Works
Last day. The count for November is 30, so new DateTime(2026, 11, 30) is the final day.
Loop 1 to 12. Adding each month together gives the number of days in the whole year.
365. 2026 is not a leap year, so the total is 365.
Month 13. It is not valid, so ArgumentOutOfRangeException is thrown and caught.
Frequently Asked Questions
Use new DateTime(year, month, DateTime.DaysInMonth(year, month)).
Yes. DaysInMonth(2028, 2) returns 29, and DaysInMonth(2026, 2) returns 28.
It throws ArgumentOutOfRangeException. The month must be 1 to 12.
Yes. Call it as DateTime.DaysInMonth(year, month); you do not need a DateTime object.
No. Century years must be divisible by 400, so February 2100 has 28 days.