我正试着赶上当月的最后一天和下个月的最后一天。
这是我到目前为止提出的代码,但是我被迫在NodaTime代码和.NET方法DateTime.DaysInMonth()之间混合以实现我正在寻找的东西,这听起来不对
class Program {
public static void Main(string[] args)
{
DateTimeZone mst = DateTimeZoneProviders.Tzdb["MST"];
Instant now = SystemClock.Instance.Now;
ZonedDateTime mstNow = now.InZone(mst);
LocalDate mstLastDayOfCurrentMonth = new LocalDate(mstNow.Year, mstNow.Month, DateTime.DaysInMonth(mstNow.Year, mstNow.Month));
Console.WriteLine("Last Day of Current Month: {0}", mstLastDayOfCurrentMonth);
//move into the next month
LocalDate nextMonth = mstLastDayOfCurrentMonth.PlusDays(1);
LocalDate mstLastDayOfNextMonth = new LocalDate(nextMonth.Year, nextMonth.Month, DateTime.DaysInMonth(nextMonth.Year, nextMonth.Month));
Console.WriteLine("Last Day of Next Month: {0}", mstLastDayOfNextMonth);
}
}
您能告诉我NodaTime推荐的当前和未来几个月的最后一天是什么方式吗?
提前致谢
答案 0 :(得分:8)
获取ZonedDateTime
的{{3}},然后调用Calendar
方法:
ZonedDateTime mstNow = now.InZone(mst);
CalendarSystem calendar = mstNow.Calendar;
LocalDate mstLastDayOfCurrentMonth = new LocalDate(
mstNow.Year, mstNow.Month, calendar.GetDaysInMonth(mstNow.Year, mstNow.Month));