471

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)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gold
  • 60,526
  • 100
  • 215
  • 315
  • 2
    @Mark: What for may I ask? Your own answer doesn't require an extension method, I think. – abatishchev Mar 22 '10 at 14:48
  • 4
    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 Mar 22 '10 at 14:49
  • @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 Mar 22 '10 at 14:50

16 Answers16

879

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

DateTime.DaysInMonth(1980, 08);
Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
  • 47
    public static DateTime ConvertToLastDayOfMonth(DateTime date) { return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); } to get the last day of the month in a date format – regisbsb Dec 16 '14 at 00:38
  • or "new DateTime(1980,8,3).AddMonths(1).AddDays(-3);". (-3) is the day amount so 0 of next month is basicly the last day of current month. – Berker Yüceer Jul 28 '21 at 10:35
227
var lastDayOfMonth = DateTime.DaysInMonth(date.Year, date.Month);
Mark
  • 9,966
  • 7
  • 37
  • 39
  • @Henk Actually I pulled this from a place in our source that creates the `DateTime` from the `lastDayOfMonth`. Honestly either way works perfectly well. It's a pedantic argument which way is better. I've done it both ways and both yield the same answer. – Mark Mar 22 '10 at 15:02
78

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));
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ian G
  • 29,468
  • 21
  • 78
  • 92
23

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Radu094
  • 28,068
  • 16
  • 63
  • 80
11

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mash
  • 119
  • 1
  • 3
9
// 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));
Jasper Risseeuw
  • 1,207
  • 11
  • 11
  • This answer responds only to the questioner's example. A more comprehensive answer would be more helpful. – Scott Lawrence Oct 12 '18 at 14:16
  • You can use any date you want as input value, so you can apply this solution to any date you want. – Jasper Risseeuw Oct 15 '18 at 08:02
  • 1
    It will fail for some dates, try `var myDate = new DateTime(1980, 1, 31);` (returns the 29th) – Hans Kesting Oct 15 '18 at 08:26
  • Hans Kesting, you are right. This method will fail if the next month has less days than the current. I guess the easiest way is to use DateTime.DaysInMonth() but that is the accepted answer so my answer should be deleted. – Jasper Risseeuw Oct 15 '18 at 09:00
  • 1
    using .net 6 - no it doesn't (anymore?). new DateTime(1980, 1, 31) returns 1980-01-31 correctly with above method. – Morten_564834 Mar 25 '22 at 08:51
6

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;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jithin
  • 93
  • 1
  • 2
  • It is wondering me that instead of simple method: `DateTime.DaysInMonth`, why someone should look for this unreadable and complex way to achieve it!? - But as a valid solution is acceptable ;). – shA.t Sep 10 '17 at 09:11
6

You can extend DateTime as follows;

public static class DateTimeMethods
{
    public static DateTime StartOfMonth(this DateTime date)
    {
        return new DateTime(date.Year, date.Month, 1, 0, 0, 0);
    }

    public static DateTime EndOfMonth(this DateTime date)
    {
        return date.StartOfMonth().AddMonths(1).AddSeconds(-1);
    }
}

and use it as follows;

DateTime today = DateTime.Now;

DateTime startOfMonth = today.StartOfMonth();
DateTime endOfMonth = today.EndOfMonth();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
4

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));
Md. Shafiqur Rahman
  • 2,878
  • 27
  • 24
  • Why this has been upvoted I do not know - there is no reason whatsoever to involve a UI control when the `DateTime` object has provided more than adequate support to solve this problem for over a decade. – Steve Pettifer Sep 30 '22 at 12:40
  • I have mentioned it is using DateTimePicker. As I have tried to get first and last date of the month selected in the DateTimePicker UI – Md. Shafiqur Rahman Oct 13 '22 at 05:34
  • And it was still utterly irrelevant to the question. Answers like this should be deleted as they can be misleading to inexperienced developers. – Steve Pettifer Oct 14 '22 at 07:07
3

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)
3

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

  • No, I tried it and for some dates it does not work. For example on 2023/01/31 : AddMonths(+1) brings you to 2023/02/28, then you remove 31 days and the result is... 2023/01/28 (which is not what we wanted ;) ). – katter75 Jan 31 '23 at 10:24
2

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;
}
shA.t
  • 16,580
  • 5
  • 54
  • 111
0

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;
    }
Gauravsa
  • 6,330
  • 2
  • 21
  • 30
0

I only wanted to fire code on the last day of every month, it's a simple as this...

if (DateTime.UtcNow.AddDays(1).Day != 1)
{
    // Tomorrow is not the first...
    return;
}
-1

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.

RHSeeger
  • 16,034
  • 7
  • 51
  • 41
-4

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 :)

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77