How to Get First and Last Day of Previous Month in C#?

When working with dates in C#, you might encounter a scenario where you need to calculate the first or last day of the previous month. In this tutorial, we’ll walk through how to get the first and last day of the previous month in C#.

To get the first and last day of the previous month in C#, use the DateTime class. First, determine the first day of the current month, then subtract one day to get the last day of the previous month. For the first day of the previous month, subtract the number of days in the last month from its last day. This method accounts for varying month lengths and leap years.

Get the First Day of the Previous Month in C#

To find the first day of the previous month in C#, we need to perform a couple of simple date manipulations:

  1. Start with the current date.
  2. Move to the first day of the current month.
  3. Then subtract one day to go to the previous month.
  4. Finally, set the day to 1 to get the first day of that month.

Here’s how you can do it in C#:

DateTime currentDate = DateTime.Now; // Step 1: Get the current date

DateTime firstDayCurrentMonth = new DateTime(currentDate.Year, currentDate.Month, 1);// Step 2: Move to the first day of the current month

DateTime lastDayPreviousMonth = firstDayCurrentMonth.AddDays(-1);// Step 3: Subtract one day to go to the previous month

DateTime firstDayPreviousMonth = new DateTime(lastDayPreviousMonth.Year, lastDayPreviousMonth.Month, 1);// Step 4: Set the day to 1 to get the first day

Now firstDayPreviousMonth holds the value of the first day of the previous month.

Here is the complete C# program to get the first day of the previous month in C#:

using System;

namespace DateManipulation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the current date
            DateTime currentDate = DateTime.Now;

            // Move to the first day of the current month
            DateTime firstDayCurrentMonth = new DateTime(currentDate.Year, currentDate.Month, 1);

            // Subtract one day to go to the previous month
            DateTime lastDayPreviousMonth = firstDayCurrentMonth.AddDays(-1);

            // Set the day to 1 to get the first day of the previous month
            DateTime firstDayPreviousMonth = new DateTime(lastDayPreviousMonth.Year, lastDayPreviousMonth.Month, 1);

            // Display the result
            Console.WriteLine("The first day of the previous month was: " + firstDayPreviousMonth.ToShortDateString());
        }
    }
}

Here you can see the screenshot below for the output after I ran the code using the Visual Studio console application.

Get the First Day of the Previous Month in C#

Get the Last Day of the Previous Month in C#

The last day of the previous month is just one day before the first day of the current month. So, you can calculate it by subtracting one day from the first day of the current month:

DateTime currentDate = DateTime.Now; // Get the current date

DateTime firstDayCurrentMonth = new DateTime(currentDate.Year, currentDate.Month, 1); // Move to the first day of the current month

DateTime lastDayPreviousMonth = firstDayCurrentMonth.AddDays(-1); // Subtract one day to get the last day of the previous month

lastDayPreviousMonth will now contain the date for the last day of the previous month.

Here is the complete code to get the last day of the previous month in C#.

using System;

namespace DateManipulation
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the current date
            DateTime currentDate = DateTime.Now;

            // Move to the first day of the current month
            DateTime firstDayCurrentMonth = new DateTime(currentDate.Year, currentDate.Month, 1);

            // Subtract one day to get the last day of the previous month
            DateTime lastDayPreviousMonth = firstDayCurrentMonth.AddDays(-1);

            // Display the result
            Console.WriteLine("The last day of the previous month was: " + lastDayPreviousMonth.ToShortDateString());
        }
    }
}

Once I ran the code using the Visual Studio console application, you can see the output in the screenshot below:

Get the Last Day of the Previous Month in C#

Conclusion

You’ve just learned how to calculate the first and last day of the previous month in C#. Remember that date manipulation can be affected by time zones and daylight saving time, so always consider the context in which your code will run.

By following the steps provided in this tutorial, you should be able to know how to Get First and Last Day of Previous Month in C#.

You may also like: