385
votes

How can I find the last day of the month in C#?

For example, if I have the date 03/08/1980, how do I get the last day of month 8 (in this case 31)?

14
@Mark: What for may I ask? Your own answer doesn't require an extension method, I think.abatishchev
The last day is not specific to the month only, you need the year also. The last day of february 2010 is 28, but the last day of february 2008 is 29.Guffa
@abatishchev It doesn't require an extension method, but the question doesn't really ask for it. However, it is a lot nicer and much more readable, at least to me, to see it one. The extension method was more of a suggestion than anything. Any solution would work in an extension method, not just mine.Mark

14 Answers

737
votes

The last day of the month you get like this, which returns 31:

DateTime.DaysInMonth(1980, 08);
194
votes
var lastDayOfMonth = DateTime.DaysInMonth(date.Year, date.Month);
53
votes

If you want the date, given a month and a year, this seems about right:

public static DateTime GetLastDayOfMonth(this DateTime dateTime)
{
    return new DateTime(dateTime.Year, dateTime.Month, DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
}
12
votes

Substract a day from the first of next month:

DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month+1,1).AddDays(-1);

Also, in case you need it to work for December too:

DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month,1).AddMonths(1).AddDays(-1);
6
votes

You can find the last date of any month by this code:

var now = DateTime.Now;
var startOfMonth = new DateTime(now.Year, now.Month, 1);
var DaysInMonth = DateTime.DaysInMonth(now.Year, now.Month);
var lastDay = new DateTime(now.Year, now.Month, DaysInMonth);
6
votes

You can find the last day of the month by a single line of code:

int maxdt = (new DateTime(dtfrom.Year, dtfrom.Month, 1).AddMonths(1).AddDays(-1)).Day;
5
votes

From DateTimePicker:

First date:

DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);

Last date:

DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));
5
votes
// Use any date you want, for the purpose of this example we use 1980-08-03.
var myDate = new DateTime(1980,8,3);
var lastDayOfMonth = new DateTime(myDate.Year, myDate.Month, DateTime.DaysInMonth(myDate.Year, myDate.Month));
2
votes

To get last day of a month in a specific calendar - and in an extension method -:

public static int DaysInMonthBy(this DateTime src, Calendar calendar)
{
    var year = calendar.GetYear(src);                   // year of src in your calendar
    var month = calendar.GetMonth(src);                 // month of src in your calendar
    var lastDay = calendar.GetDaysInMonth(year, month); // days in month means last day of that month in your calendar
    return lastDay;
}
1
votes

This will display the last date of the next month. You can add the month of the year you want to return by adding or substracting it from AddMonths(x)

DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day)
0
votes

This formula reflects @RHSeeger's thought as a simple solution to get (in this example) the last day of the 3rd month (month of date in cell A1 + 4 with the first day of that month minus 1 day):

=DATE(YEAR(A1);MONTH(A1)+4;1)-1

Very precise, inclusive February's in leap years :)

0
votes

example on 05/07/2021

DateTime.Now.Day;

Result: 5

DateTime.Now.AddMonths(+1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd");

result: 2021/07/31

0
votes

Another way to get end date:

    private static DateTime GetMonthEndDate(DateTime date)
    {
        DateTime endDate = date;
        int endDateMonth = endDate.Month;

        while (endDateMonth == endDate.Month)
            endDate = endDate.AddDays(1);

        endDate = endDate.AddDays(-1);

        return endDate;
    }
-1
votes

I don't know C# but, if it turns out there's not a convenient API way to get it, one of the ways you can do so is by following the logic:

today -> +1 month -> set day of month to 1 -> -1 day

Of course, that assumes you have date math of that type.