C# Tips & Tricks #24 – Find Last Day of Month

There are times when you might want to find out what is the last day of a given month in C#.

How to get the Last Day of the Month in C# ?

You could easily derive it by using the Number of Days in a Month of a Year. The Year componenr is extremely important because of the Leap Year. Eg : The Last day in Feb 2006 is different from Last day in Feb 2004.

var result = DateTime.DaysInMonth(2020, 10);

Here’s a full sample code snippet with working example demonstrating how to find the Last day of the month in C#.

using System;
public class Hello {
    public static void Main() {
        var result = DateTime.DaysInMonth(2020, 10);
        System.Console.WriteLine("Last Day of the Month: "+ result);
    }
}

Output

Last Day in the Month: 31

Copy link
Powered by Social Snap