[英]How do I get the last day of a month?
如何在 C# 中找到一个月的最后一天?
例如,如果我有日期 03/08/1980,我如何获得第 8 个月的最后一天(在本例中为 31)?
你得到这样的一个月的最后一天,它返回 31:
DateTime.DaysInMonth(1980, 08);
var lastDayOfMonth = DateTime.DaysInMonth(date.Year, date.Month);
DateTime firstOfNextMonth = new DateTime(date.Year, date.Month, 1).AddMonths(1);
DateTime lastOfThisMonth = firstOfNextMonth.AddDays(-1);
如果你想要date ,给定一个月和一年,这似乎是正确的:
public static DateTime GetLastDayOfMonth(this DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
}
从下个月的第一天减去一天:
DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month+1,1).AddDays(-1);
此外,如果您也需要它在 12 月工作:
DateTime lastDay = new DateTime(MyDate.Year,MyDate.Month,1).AddMonths(1).AddDays(-1);
您可以通过以下代码找到任何月份的最后日期:
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));
您可以通过一行代码找到该月的最后一天:
int maxdt = (new DateTime(dtfrom.Year, dtfrom.Month, 1).AddMonths(1).AddDays(-1)).Day;
从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));
您可以按如下方式扩展 DateTime;
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);
}
}
并按如下方式使用它;
DateTime today = DateTime.Now;
DateTime startOfMonth = today.StartOfMonth();
DateTime endOfMonth = today.EndOfMonth();
要在特定日历和扩展方法中获取一个月的最后一天:
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;
}
这将显示下个月的最后一个日期。 您可以通过从 AddMonths(x) 中添加或减去它来添加要返回的一年中的月份
DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day)
2021 年 5 月 7 日的示例
日期时间.现在.日;
结果:5
DateTime.Now.AddMonths(+1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd");
结果:2021/07/31
另一种获取结束日期的方法:
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;
}
我只想在每个月的最后一天触发代码,就这么简单......
if (DateTime.UtcNow.AddDays(1).Day != 1)
{
// Tomorrow is not the first...
return;
}
我不知道 C# 但是,如果事实证明没有方便的 API 方法来获取它,那么您可以这样做的一种方法是遵循以下逻辑:
today -> +1 month -> set day of month to 1 -> -1 day
当然,这假设你有这种类型的日期数学。
这个公式反映了@RHSeeger 的想法,它是获取(在本例中)第 3 个月的最后一天(单元格 A1 + 4 中的日期月份,该月的第一天减去 1 天)的简单解决方案:
=DATE(YEAR(A1);MONTH(A1)+4;1)-1
非常精确,包括闰年的二月:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.