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

前端 未结 10 2077
独厮守ぢ
独厮守ぢ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 12:04

    I like V4V's answer, but I write it this way:

    DateTime dt = new DateTime(2011,12,2);
    DateTime firstDayNextMonth = dt.AddMonths(1).AddDays(-dt.Day+1);
    

    For example, I might be computing a future time and this code does that without stripping out the time part.

    Per hvd's most astute comment, this code should be:

    DateTime dt = new DateTime(2011,12,2);
    DateTime firstDayNextMonth = dt.AddDays(-dt.Day+1).AddMonths(1);
    

提交回复
热议问题