Subtract one month from Datetime.Today

后端 未结 3 588
醉梦人生
醉梦人生 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:20

    Patrick got it. To build on his answer and improve error handling (if there's a possibility that qs could be an invalid date string), you might do something like:

    DateTime qsValue;
    
    dateTimePicker1.MaxDate = DateTime.Today.AddMonths(-1);
    
    dateTimePicker1.Value = (DateTime.TryParse(qs, out qsValue))
        ? qsValue
        : dateTimePicker1.MaxDate;
    

提交回复
热议问题