Programming, ASP.NET, C#, .Net

C# Getting the Prior Month Start and End Dates with the Correct Times

It seems like a simple task, but can end up being a bit tricky depending on how you write your code. I’ve seen a lot of examples of getting the start date and end date for a month but a lot of them are inaccurate or even incomplete and fail to take certain situations into account. In this article I’ll go through an example of playing around with getting the start day and end day of the previous month along with finding the right starting and ending times.

When you are coding to calculate the start and end of the previous month there are several factors your code will have to take into consideration.

Don’t try to Calculate things Yourself

For starters, you shouldn’t try to manually compute the ending day of each month. There are a lot of exceptions to the calendar, especially leap years. Using the inbuilt DateTime function will save you a lot of headache.

Another reason not to try to calculate the dates yourself is the year transition. Your code will need to consider that if you are in January, you’ll want to have the start and end dates of the December of  the previous year returned.

Consider the Time Part of the Date Range

A related point is that when you are using the DateTime function to get the last day of the month the time needs to be accurate as well. You can easily go wrong and end up with 12:00 AM of the last day of the month (in other words, the start of the last day. Depending on your needs this may be a problem if you want to account for the full last day.(to get the full last day you want 11:59PM).

The Example Code

In the example code below we are setting our current date to January 11 2020. We expect the code to return us the prior month’s start and end dates and times. In other words, we would want to see the start date as December 1st 2019 12AM and the end date as December 31st 2019 11:59PM.

Here is what the code will output:

DateTime dtToday = new DateTime(2020,1,11);
// DateTime today = DateTime.Today;
DateTime dtThisMonthStart = new DateTime(dtToday.Year, dtToday.Month, 1);
DateTime dtFirst = dtThisMonthStart.AddMonths(-1);
DateTime dtLast = dtThisMonthStart.AddDays(-1);
Console.WriteLine("Start dt (Midnight): " + dtFirst);
Console.WriteLine("End dt (Wrong time): " + dtLast);
DateTime dtLastTime = new DateTime(dtLast.Year, dtLast.Month, 1).AddMonths(1).AddSeconds(-1);
Console.WriteLine("Correct End dt & Time: " + dtLastTime);

In the code above, notice that we care about time and correct the time part of the end of the month date by going forward a month (the first of this month) and then subtracting a second in order to get the last instance of the prior month.

If you don’t actually care about the times it gets a lot simpler. You can output each date part as:


2 thoughts on “C# Getting the Prior Month Start and End Dates with the Correct Times”

  1. In your code “dtLastMonth” is *this* month….(algorithm, looks almost right, but variable is wrong name)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s