如何使用DaysInMonth
:
DateTime createDate = new DateTime (year, month, DateTime.DaysInMonth(year, month));
(注意自己 - 必须在Noda Time中轻松实现......)
如何使用DaysInMonth
:
DateTime createDate = new DateTime (year, month, DateTime.DaysInMonth(year, month));
(注意自己 - 必须在Noda Time中轻松实现......)
您可以使用该方法DateTime.DaysInMonth(year,month)
获取任何给定月份的天数.
这是我在CodePlex上有用的DateTime扩展库中找到的一种优雅方法:
http://datetimeextensions.codeplex.com/
这是一些示例代码:
public static DateTime First(this DateTime current) { DateTime first = current.AddDays(1 - current.Day); return first; } public static DateTime First(this DateTime current, DayOfWeek dayOfWeek) { DateTime first = current.First(); if (first.DayOfWeek != dayOfWeek) { first = first.Next(dayOfWeek); } return first; } public static DateTime Last(this DateTime current) { int daysInMonth = DateTime.DaysInMonth(current.Year, current.Month); DateTime last = current.First().AddDays(daysInMonth - 1); return last; }
它还有一些其他有用的扩展,可能对您有所帮助.