2011-01-20 53 views

回答

24

不需要自定義計算。

使用System.DateTime.DaysInMonth(yearNum, monthNum)方法找出任​​何給定月份(也是最後一天)的天數。

這是簡單:

//Get days in month 2 (Feb) of year 2011. Returns 28. 
int daysInFeb2011 = System.DateTime.DaysInMonth(2011, 2); 

MSDN文檔提供了一個更徹底的和描述性的樣品:

 const int July = 7; 
     const int Feb = 2; 

     // daysInJuly gets 31. 
     int daysInJuly = System.DateTime.DaysInMonth(2001, July); 

     // daysInFeb gets 28 because the year 1998 was not a leap year. 
     int daysInFeb = System.DateTime.DaysInMonth(1998, Feb); 

     // daysInFebLeap gets 29 because the year 1996 was a leap year. 
     int daysInFebLeap = System.DateTime.DaysInMonth(1996, Feb); 
相關問題