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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...