how could i programmatically detect, how many days are ther开发者_运维百科e for a particular month & year.
It's already there:
DateTime.DaysInMonth(int year, int month);
should do it.
Something like this should do what you want:
static int GetDaysInMonth(int year, int month) {
DateTime dt1 = new DateTime(year, month, 1);
DateTime dt2 = dt1.AddMonths(1);
TimeSpan ts = dt2 - dt1;
return (int)ts.TotalDays;
}
You get the first day of the month, add one month and count the days in between.
精彩评论