How to find the first day of next month,if the current month is december

前端 未结 10 2074
独厮守ぢ
独厮守ぢ 2021-01-01 11:03

I\'m using the following query to get the next month.

int theMonth = ((System.DateTime)periodStartDate).Month+1;

But if the periodstartDate

10条回答
  •  太阳男子
    2021-01-01 12:01

    The expression ((System.DateTime)periodStartDate).Month+1 doesn't throw an error if the month is December - it just returns 13. I suspect you're doing this:

    var nextMonth = new DateTime(periodStartDate.Year, periodStartDate.Month + 1, 1);
    

    That would throw an error.

    Try this instead:

    var nextMonth = new DateTime(periodStartDate.Year, periodStartDate.Month, 1)
        .AddMonths(1);
    

提交回复
热议问题