Subtract one month from Datetime.Today

后端 未结 3 587
醉梦人生
醉梦人生 2021-02-06 21:56

I have a DateTimePicker in which I allow user to select month previous to the current year.

The problem is, that if the date is 1st January, it can\'t calc

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 22:08

    If you, like Adil H. Raza, don't like to have negative numbers in your code, you could also make an extension method:

    public static DateTime SubtractMonths(this DateTime dt, int months) => dt.AddMonths(-months);
    

    And use it like

    var lastmonth = DateTime.Today.SubtractMonths(1);
    

提交回复
热议问题