Yog*_*esh 346
另一种方式:
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year,
today.Month,
DateTime.DaysInMonth(today.Year,
today.Month));
Run Code Online (Sandbox Code Playgroud)
Set*_*eth 63
就像是:
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, today.Month, 1).AddMonths(1).AddDays(-1);
Run Code Online (Sandbox Code Playgroud)
也就是说,你得到下个月的第一天,然后减去一天.框架代码将处理月份长度,闰年等事情.
Mar*_*inC 10
public static class DateTimeExtensions
{
public static DateTime LastDayOfMonth(this DateTime date)
{
return date.AddDays(1-(date.Day)).AddMonths(1).AddDays(-1);
}
}
Run Code Online (Sandbox Code Playgroud)
DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)
Run Code Online (Sandbox Code Playgroud)