开发者

How do I get the last day of a month?

开发者 https://www.devze.com 2022-12-23 23:30 出处:网络
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)?The last day of the month you get l

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


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

DateTime.DaysInMonth(1980, 08);


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


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


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


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


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


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;


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


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


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)


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


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


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


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号