开发者

Getting number of days for a specific month

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜