DateTime.DaysInMonth() Method in C#

The DateTime.DaysInMonth() method in C# returns the number of days in the specified month and year. This static method is particularly useful for calendar calculations and date validations, automatically handling leap years and different month lengths.

Syntax

Following is the syntax for the DateTime.DaysInMonth() method −

public static int DaysInMonth(int year, int month);

Parameters

  • year − An integer representing the year (1 to 9999).

  • month − An integer representing the month (1 to 12).

Return Value

Returns an int representing the number of days in the specified month and year. The method automatically accounts for leap years when calculating February days.

Using DaysInMonth() for Different Months

Example

using System;

public class Demo {
    public static void Main() {
        DateTime date1 = new DateTime(2019, 08, 20, 6, 20, 40);
        DateTime date2 = new DateTime(2019, 06, 20, 6, 20, 40);
        
        Console.WriteLine("DateTime 1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", date1);
        Console.WriteLine("Days in DateTime 1 month = " + DateTime.DaysInMonth(2019, 08));
        
        Console.WriteLine("\nDateTime 2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", date2);
        Console.WriteLine("Days in DateTime 2 month = " + DateTime.DaysInMonth(2019, 06));
        
        int res = date1.CompareTo(date2);
        Console.WriteLine("\nReturn Value (comparison) = " + res);
    }
}

The output of the above code is −

DateTime 1 = 20 August 2019, 06:20:40
Days in DateTime 1 month = 31
DateTime 2 = 20 June 2019, 06:20:40
Days in DateTime 2 month = 30
Return Value (comparison) = 1

Using DaysInMonth() for Leap Year Calculation

Example

using System;

public class Demo {
    public static void Main() {
        int year1 = 2019, year2 = 2016;
        int FebMonth = 2;
        
        Console.WriteLine("Days in 2019, Feb month = " + DateTime.DaysInMonth(year1, FebMonth));
        Console.WriteLine("Days in 2016, Feb month = " + DateTime.DaysInMonth(year2, FebMonth));
        
        // Check if years are leap years
        Console.WriteLine("Is 2019 a leap year? " + DateTime.IsLeapYear(year1));
        Console.WriteLine("Is 2016 a leap year? " + DateTime.IsLeapYear(year2));
    }
}

The output of the above code is −

Days in 2019, Feb month = 28
Days in 2016, Feb month = 29
Is 2019 a leap year? False
Is 2016 a leap year? True

Using DaysInMonth() for Calendar Display

Example

using System;

public class Demo {
    public static void Main() {
        int currentYear = 2024;
        string[] months = {"", "January", "February", "March", "April", "May", "June",
                          "July", "August", "September", "October", "November", "December"};
        
        Console.WriteLine("Days in each month for year " + currentYear + ":");
        Console.WriteLine("----------------------------------------");
        
        for (int month = 1; month <= 12; month++) {
            int days = DateTime.DaysInMonth(currentYear, month);
            Console.WriteLine("{0,-12}: {1} days", months[month], days);
        }
    }
}

The output of the above code is −

Days in each month for year 2024:
----------------------------------------
January     : 31 days
February    : 29 days
March       : 31 days
April       : 30 days
May         : 31 days
June        : 30 days
July        : 31 days
August      : 31 days
September   : 30 days
October     : 31 days
November    : 30 days
December    : 31 days

Common Use Cases

  • Calendar Applications − Displaying correct number of days in calendar grids.

  • Date Validation − Ensuring a specific day exists in a given month and year.

  • Business Logic − Calculating end-of-month dates for billing or reporting.

  • Leap Year Handling − Automatically accounting for February's varying length.

Conclusion

The DateTime.DaysInMonth() method provides a reliable way to determine the number of days in any month and year combination. It automatically handles leap years and different month lengths, making it essential for date calculations and calendar-related applications in C#.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements