C# DateTime DaysInMonth() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

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)
CallWhat 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

ParameterDescription
yearRequired. A year from 1 to 9999.
monthRequired. A month number from 1 (January) to 12 (December).

Return Value

Return ValueDescription
intThe 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));
    }
}

How It Works

  1. November. It is one of the four months with 30 days.
  2. December. It has 31 days like January, March, May, July, August, and October.
  3. February 2026. The year is not a leap year, so the answer is 28.
  4. 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));
    }
}

How It Works

  1. 2028. It is divisible by 4 and not a century, so February has 29 days.
  2. 2100. Century years are leap only when divisible by 400, so this February has 28 days.
  3. 2000. It is divisible by 400, so it is a leap year with 29 days.
  4. 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");
        }
    }
}

How It Works

  1. Last day. The count for November is 30, so new DateTime(2026, 11, 30) is the final day.
  2. Loop 1 to 12. Adding each month together gives the number of days in the whole year.
  3. 365. 2026 is not a leap year, so the total is 365.
  4. 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.

Next: Equals()

Check if two DateTime values are the same instant (same ticks) with System.DateTime.

Equals() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful