如何在c#中找到一个月的最后一天?
例如,如果我有日期03/08/1980,我如何得到第8个月的最后一天(在本例中是31)?
如何在c#中找到一个月的最后一天?
例如,如果我有日期03/08/1980,我如何得到第8个月的最后一天(在本例中是31)?
当前回答
我不懂c#,但是,如果没有一个方便的API方法来获得它,其中一种方法是遵循以下逻辑:
today -> +1 month -> set day of month to 1 -> -1 day
当然,前提是你有这种类型的约会数学。
其他回答
另一种获取结束日期的方法:
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;
}
从DateTimePicker:
第一次约会:
DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);
最后的日期:
DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));
我只想在每个月的最后一天发射代码,就是这么简单……
if (DateTime.UtcNow.AddDays(1).Day != 1)
{
// Tomorrow is not the first...
return;
}
// 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));
这将显示下个月的最后日期。可以从AddMonths(x)中添加或减去想要返回的月份。
DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day)