我怎样才能得到在C#月的最后一天?

我怎样才能find在C#月的最后一天?

另一种方法:

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

就像是:

 DateTime today = DateTime.Today; DateTime endOfMonth = new DateTime(today.Year, today.Month, 1).AddMonths(1).AddDays(-1); 

也就是说你得到下个月的第一天,然后减去一天。 框架代码将处理月份长度,闰年和这样的事情。

 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) 

尝试这个。 它会解决你的问题。

  var lastDayOfMonth = DateTime.DaysInMonth(int.Parse(ddlyear.SelectedValue), int.Parse(ddlmonth.SelectedValue)); DateTime tLastDayMonth = Convert.ToDateTime(lastDayOfMonth.ToString() + "/" + ddlmonth.SelectedValue + "/" + ddlyear.SelectedValue);