How to Get Start and End Date of Month in C#?

When working with dates in C#, you might come across a scenario where you need to determine the first and last day of a given month. In this tutorial, we’ll go through how to calculate the start and end dates of the current month in C#.

To get the start and end dates of a month in C#, you can use the DateTime class. For the start date, create a DateTime object with the desired year and month, and set the day to 1. To find the end date, add one month to the start date and subtract one day. This approach automatically adjusts for different month lengths and leap years.

Before we dive into the code, it’s important to understand the DateTime structure in C#. In C#, DateTime is a structure that represents dates and times. It provides properties and methods to work with dates and times in a variety of formats.

Get the First Day of the Current Month in C#

To get the first day of the current month, you can use the DateTime structure to create a new date with the current year and month, but with the day set to 1.

DateTime firstDayOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

This line of code creates a new DateTime object that represents the first day of the current month. DateTime.Now provides the current local date and time, and from that, we extract the year and month. We explicitly set the day to 1.

Get the Last Day of the Current Month in C#

To find the last day of the month, you can add one month to the first day of the current month and then subtract one day.

DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);

Here, AddMonths(1) adds one month to the first day of the current month, which takes us to the first day of the next month. Then, AddDays(-1) subtracts one day, bringing us to the last day of the current month.

Get Start and End Date of Month in C# – Complete Example

Let’s put it all together in a complete example to Get the Start and End Date of the Month in C#.

using System;

namespace DateExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the first day of the current month
            DateTime firstDayOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            Console.WriteLine("First Day of Month: " + firstDayOfMonth.ToShortDateString());

            // Get the last day of the current month
            DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
            Console.WriteLine("Last Day of Month: " + lastDayOfMonth.ToShortDateString());
        }
    }
}

When you run this program, it will display the first and last days of the current month in the format MM/dd/yyyy. You can see the screenshot below for the output after I ran the code using a console application in C#.

Get Start and End Date of Month in C#

Conclusion

You’ve just learned how to get the start and end dates of the current month in C#. These operations are crucial for many applications that deal with dates. With the DateTime structure, C# makes it easy to manipulate dates and times, allowing you to focus on the logic of your application rather than the intricacies of date handling.

In this C# tutorial, I have explained how to get the start date and end date of the month in C#.

You may also like: