188

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

JavaAndCSharp
  • 1,507
  • 3
  • 23
  • 47
Gali
  • 14,511
  • 28
  • 80
  • 105
  • DateTime.DaysInMonth(1980, 08); Please see this article http://stackoverflow.com/questions/2493032/how-to-get-the-last-day-of-a-month –  Jan 29 '14 at 12:08

5 Answers5

397

Another way of doing it:

DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, 
                                   today.Month, 
                                   DateTime.DaysInMonth(today.Year, 
                                                        today.Month));
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Yogesh
  • 14,498
  • 6
  • 44
  • 69
  • 10
    I was about to suggest System.Globalization.CultureInfo.CurrentCulture.Calendar.GetDaysInMonth.GetDaysInMonth but this was way shorter. – hultqvist Jan 11 '11 at 07:40
  • Way late, but to any traveler. It may be important to specify the kind if you intend on making this more generic or an extension method in your app. `DateTime.SpecifyKind( new DateTime(/*...*/) , existingDateTime.Kind)` otherwise you risk losing the existing date time's timezone infomration. – Alex Aug 26 '21 at 19:58
76

Something like:

DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, today.Month, 1).AddMonths(1).AddDays(-1);

Which is to say that you get the first day of next month, then subtract a day. The framework code will handle month length, leap years and such things.

Seth
  • 45,033
  • 10
  • 85
  • 120
  • 1
    Exactly what I wanted to write, but thought someone would beat me to it :) +1 – leppie Jan 11 '11 at 07:22
  • 10
    Humm .. if it is repeatedly used at many places in your code, write this as an Extension method on dateTime class which you can invoke on DateTime.Now. For ex. DateTime.Now.LastDayOfMonth(); – Unmesh Kondolikar Jan 11 '11 at 07:25
  • 2
    I liked this approtch the best. However I noticed that it didn't actually get the end of the last day, it got 12am. I did this instead DateTime endOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddSeconds(-1); returns "9/30/2020 11:59:59 PM" instead of "9/30/2020 12:00:00 AM" – Skint007 Sep 04 '20 at 21:16
  • @Skint077 - Use AddMilliSeconds instead. Its a tiny bit more accurate – Michael Ceranski Sep 10 '21 at 12:47
12
public static class DateTimeExtensions
{
    public static DateTime LastDayOfMonth(this DateTime date)
    {
        return date.AddDays(1-(date.Day)).AddMonths(1).AddDays(-1);
    }
}
MartinC
  • 855
  • 10
  • 11
  • This is a great solution when you need the time part of the date to be preserved. The other solutions don't do that. I suggest to rewrite "AddDays(1-(date.Day))" to "AddDays(-date.Day + 1)" to make the intent clearer. – humbads May 01 '17 at 15:39
12
DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)
Haris N I
  • 6,474
  • 6
  • 29
  • 35
-9

try this. It will solve your problem.

 var lastDayOfMonth = DateTime.DaysInMonth(int.Parse(ddlyear.SelectedValue), int.Parse(ddlmonth.SelectedValue));
DateTime tLastDayMonth = Convert.ToDateTime(lastDayOfMonth.ToString() + "/" + ddlmonth.SelectedValue + "/" + ddlyear.SelectedValue);
Raging Bull
  • 18,593
  • 13
  • 50
  • 55
Isanka
  • 11
  • 1
  • 5
    Constructing a `string` so you can [parse it into a `DateTime`](http://msdn.microsoft.com/library/xhz1w05e.aspx#remarksToggle) is inefficient and reliant on the date formatting of the current culture. There are cleaner solutions provided by the other three-year-old answers. – Lance U. Matthews Feb 25 '14 at 05:33