您可以使用DaysInMonth函数
例如
Date.DaysInMonth(2008, 3)
Run Code Online (Sandbox Code Playgroud)
显然,你必须将年份和月份传递给函数
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
int numDays = DateTime.DaysInMonth(year, month);
Run Code Online (Sandbox Code Playgroud)