Subtract days from a DateTime

后端 未结 9 443
北海茫月
北海茫月 2021-01-30 04:59

I have the following code in my C# program.

DateTime dateForButton =  DateTime.Now;  
dateForButton = dateForButton.AddDays(-1);  // ERROR: un-representable Date         


        
9条回答
  •  一生所求
    2021-01-30 05:13

    The dateTime.AddDays(-1) does not subtract that one day from the dateTime reference. It will return a new instance, with that one day subtracted from the original reference.

    DateTime dateTime = DateTime.Now;
    DateTime otherDateTime = dateTime.AddDays(-1);
    

提交回复
热议问题