开发者

How can I get the last day of the month in C#? [duplicate]

开发者 https://www.devze.com 2023-02-03 08:50 出处:网络
This question already has answers here: How do I get the last day of a month? (16 answers) 开发者_高级运维Closed 6 years ago.
This question already has answers here: How do I get the last day of a month? (16 answers) 开发者_高级运维 Closed 6 years ago.

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


Another way of doing it:

DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, 
                                   today.Month, 
                                   DateTime.DaysInMonth(today.Year, 
                                                        today.Month));


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.


public static class DateTimeExtensions
{
    public static DateTime LastDayOfMonth(this DateTime date)
    {
        return date.AddDays(1-(date.Day)).AddMonths(1).AddDays(-1);
    }
}


DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)


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

精彩评论

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