Gold
Gold

Reputation: 62424

How do I get the last day of a month?

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

Upvotes: 503

Views: 478196

Answers (16)

Ibrahim Mohammed
Ibrahim Mohammed

Reputation: 531

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

Upvotes: 10

Kieran O'Sullivan
Kieran O'Sullivan

Reputation: 11

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

Upvotes: 1

Gauravsa
Gauravsa

Reputation: 6514

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

Upvotes: 0

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

Upvotes: 3

Stefan Taban
Stefan Taban

Reputation: 31

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)

Upvotes: 3

IamWhoIam
IamWhoIam

Reputation: 1

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

Upvotes: -5

Jasper Risseeuw
Jasper Risseeuw

Reputation: 1256

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

Upvotes: 11

shA.t
shA.t

Reputation: 16958

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

Upvotes: 2

Md. Shafiqur Rahman
Md. Shafiqur Rahman

Reputation: 3058

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

Upvotes: 4

jithin
jithin

Reputation: 101

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;

Upvotes: 6

mash
mash

Reputation: 119

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

Upvotes: 11

Ian G
Ian G

Reputation: 30234

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

Upvotes: 83

Radu094
Radu094

Reputation: 28414

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

Upvotes: 25

Oskar Kjellin
Oskar Kjellin

Reputation: 21860

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

DateTime.DaysInMonth(1980, 08);

Upvotes: 931

RHSeeger
RHSeeger

Reputation: 16262

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.

Upvotes: -1

Mark
Mark

Reputation: 10206

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

Upvotes: 245

Related Questions