2011-01-20 42 views

Trả lời

24

Không cần tính toán tùy chỉnh.

Sử dụng phương thức System.DateTime.DaysInMonth(yearNum, monthNum) để tìm hiểu số ngày trong bất kỳ tháng cụ thể nào (cũng là ngày cuối cùng).

Nó đơn giản như:

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

Các tài liệu MSDN cung cấp một mẫu kỹ lưỡng hơn và mô tả:

 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); 
Các vấn đề liên quan