The last day of the month is the number of days in that month. To get the number of days in a month, use DateTime.DaysInMonth(year, month):
Console.WriteLine($"Feb 2022 has {DateTime.DaysInMonth(2022, 2)} days");
Console.WriteLine($"Feb 2024 has {DateTime.DaysInMonth(2024, 2)} days");
Code language: C# (cs)
This outputs the following:
Feb 2022 has 28 days
Feb 2024 has 29 days
Code language: plaintext (plaintext)
Notice that it handles leap years (2024) appropriately.
Using the number of days in the month, you can get the last day of the month:
int daysInMonth = DateTime.DaysInMonth(year: 2022, month: 2);
var lastDayOfMonth = new DateTime(2022, 2, daysInMonth);
Console.WriteLine($"Last day of month = {lastDayOfMonth:yyyy-MM-dd}");
Code language: C# (cs)
This outputs the last day of the month using a custom date format.
Last day of month = 2022-02-28
Code language: plaintext (plaintext)
You can use this as the basis for other date calculations, such as getting the start and end dates of a month.
Table of Contents
Getting a month’s start and end date
Often when you need to get the last day of the month, you also want to get the first day of the month. Furthermore, it’s convenient to be able to pass in a DateTime parameter instead of having to pass in a year and month. The following method takes a date and returns a named tuple with the start/end dates:
public static (DateTime startOfMonth, DateTime endOfMonth) GetMonthStartAndEnd(DateTime date)
{
DateTime startOfMonth = new DateTime(date.Year, date.Month, day: 1);
int daysInMonth = DateTime.DaysInMonth(date.Year, date.Month);
DateTime endOfMonth = new DateTime(date.Year, date.Month, daysInMonth);
return (startOfMonth, endOfMonth);
}
Code language: C# (cs)
I’ll show examples of using this method to get the start and end dates for the current month, the previous month, and the next month.
Current month’s start and end dates
First, use DateTime.Now to get the current date. Then pass this into GetMonthStartAndEnd() to get the current month’s start and end dates:
var now = DateTime.Now;
(DateTime startOfMonth, DateTime endOfMonth) = DateCalculations.GetMonthStartAndEnd(now);
Console.WriteLine($"From {startOfMonth:yyyy-MM-dd} to {endOfMonth:yyyy-MM-dd}");
Code language: C# (cs)
This outputs:
From 2022-02-01 to 2022-02-28
Code language: plaintext (plaintext)
Previous month’s start and end dates
Subtract a month with AddMonths(-1) to get the previous month. Then pass this into GetMonthStartAndEnd() to get the previous month’s start and end dates:
var previousMonth = DateTime.Now.AddMonths(-1);
(DateTime startOfMonth, DateTime endOfMonth) = DateCalculations.GetMonthStartAndEnd(previousMonth);
Console.WriteLine($"Previous month is from {startOfMonth:yyyy-MM-dd} to {endOfMonth:yyyy-MM-dd}");
Code language: C# (cs)
This outputs:
Previous month is from 2022-01-01 to 2022-01-31
Code language: plaintext (plaintext)
Next month’s start and end dates
Add a month with AddMonths(1) to get the next month. Pass this into GetMonthStartAndEnd() to get the next month’s start and end dates:
var nextMonth = DateTime.Now.AddMonths(1);
(DateTime startOfMonth, DateTime endOfMonth) = DateCalculations.GetMonthStartAndEnd(nextMonth);
Console.WriteLine($"Next month is from {startOfMonth:yyyy-MM-dd} to {endOfMonth:yyyy-MM-dd}");
Code language: C# (cs)
This outputs:
Next month is from 2022-03-01 to 2022-03-31
Code language: plaintext (plaintext)
Always use built-in methods for date calculations
There are lots of edge cases involved with dates/times. Different months have a different number of days (and February has 1 extra day on leap years). Because of this, it’s best to use built-in DateTime methods when doing date calculations. Here’s an example of an edge case – subtracting 1 month from March 30:
var month = new DateTime(2022, 3, 30).AddMonths(-1);
Console.WriteLine($"{month:yyyy-MM-dd}");
Code language: C# (cs)
This outputs the following, showing that subtracting 1 month from March 30 correctly gives you February 28:
2022-02-28
Code language: plaintext (plaintext)
Last day of previous month shortcut
If you just need the last day of the previous month, you can subtract the day from the current date with AddDays():
var month = new DateTime(year: 2022, month: 2, day: 4);
var lastDayOfPreviousMonth = month.AddDays(-month.Day);
Console.WriteLine($"Last day of previous month = {lastDayOfPreviousMonth:yyyy-MM-dd}");
Code language: C# (cs)
Note: This is a shortcut alternative to the more verbose AddMonths(-1) + DateTime.DaysInMonth() approach shown earlier.
This outputs:
Last day of previous month = 2022-01-31
Code language: plaintext (plaintext)