2014-02-14 3 views
3

나는 이번 달의 마지막 날과 다음 달의 마지막 날을 얻으려고합니다.이번 달의 마지막 날과 다음 달의 마지막 날을 얻는 방법

여기까지 코드를 작성했지만 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 권장 방식이 무엇인지 알려주십시오.

덕분에 사전에

답변

6

은 당신의 ZonedDateTimeCalendar 다음은 GetDaysInMonth(year, month) 메소드를 호출 받기 :

ZonedDateTime mstNow = now.InZone(mst); 
CalendarSystem calendar = mstNow.Calendar; 
LocalDate mstLastDayOfCurrentMonth = new LocalDate(
    mstNow.Year, mstNow.Month, calendar.GetDaysInMonth(mstNow.Year, mstNow.Month)); 
+0

확실하지 내가 놓친 어떻게하지만 좋은 작품, 감사합니다. – FullOfQuestions

관련 문제