Answers:
int days = DateTime.DaysInMonth(year, month);
显然,它随年份而变化,因为有时2月为28天,有时为29天。如果您想将其“固定”为一个值或另一个值,则始终可以选择一个特定的年份(是否跨越)。
从代码示例使用System.DateTime.DaysInMonth:
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);
为了找到一个月中的天数,DateTime类提供了一种方法“ DaysInMonth(int year,int month)”。 此方法返回指定月份中的总天数。
public int TotalNumberOfDaysInMonth(int year, int month)
{
return DateTime.DaysInMonth(year, month);
}
要么
int days = DateTime.DaysInMonth(2018,05);
输出:-31
int days = DateTime.DaysInMonth(int year,int month);
要么
int days=System.Globalization.CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(int year,int month);
您必须通过年月,int
否则月中的天数将在年月日归还