How do I get the last day of a month?

C#.NetDatetime

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

DateTime.DaysInMonth(1980, 08);

Solution 2 - C#

var lastDayOfMonth = DateTime.DaysInMonth(date.Year, date.Month);

Solution 3 - C#

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));
}

Solution 4 - C#

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

Solution 5 - C#

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

Solution 6 - C#

// 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));

Solution 7 - C#

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;

Solution 8 - C#

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

Solution 9 - C#

You can extend DateTime as follows as;

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();

Solution 10 - C#

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;
}

Solution 11 - C#

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)

Solution 12 - C#

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

Solution 13 - C#

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;
    }

Solution 14 - C#

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;
}

Solution 15 - C#

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.

Solution 16 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGoldView Question on Stackoverflow
Solution 1 - C#Oskar KjellinView Answer on Stackoverflow
Solution 2 - C#MarkView Answer on Stackoverflow
Solution 3 - C#Ian GView Answer on Stackoverflow
Solution 4 - C#Radu094View Answer on Stackoverflow
Solution 5 - C#mashView Answer on Stackoverflow
Solution 6 - C#Jasper RisseeuwView Answer on Stackoverflow
Solution 7 - C#jithinView Answer on Stackoverflow
Solution 8 - C#Md. Shafiqur RahmanView Answer on Stackoverflow
Solution 9 - C#Ibrahim MohammedView Answer on Stackoverflow
Solution 10 - C#shA.tView Answer on Stackoverflow
Solution 11 - C#Stefan TabanView Answer on Stackoverflow
Solution 12 - C#Gabriel Guzman RodriguezView Answer on Stackoverflow
Solution 13 - C#GauravsaView Answer on Stackoverflow
Solution 14 - C#Kieran O'SullivanView Answer on Stackoverflow
Solution 15 - C#RHSeegerView Answer on Stackoverflow
Solution 16 - C#IamWhoIamView Answer on Stackoverflow