开发者

Getting number of days for a specific month

开发者 https://www.devze.com 2023-02-10 07:56 出处:网络
how could i programmatically detect, how many days are ther开发者_运维百科e for a particular month & year. It\'s already there:

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

精彩评论

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