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

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...