How to get previous day using datetime

前端 未结 6 1052
日久生厌
日久生厌 2020-12-30 01:40

I want to set a DateTime property to previous day at 00:00:00. I don\'t know why DateTime.AddDays(-1) isn\'t working. Or why DateTime.AddTicks(-1) isn\'t working. First sho

6条回答
  •  时光取名叫无心
    2020-12-30 02:10

    the easiest way is this..

    DateTime yesterday = DateTime.Now.Date.AddDays(-1);
    

    now if you are trying to use a variable that has already been created, you would do this...

    DateTime yesterday = DateTime.Now;  // will give you today's date
    yesterday = yesterday.Date.AddDays(-1); // will give you yesterday's date at 12:00 AM
    

    Possibly posting your code will show us what you are doing wrong.

提交回复
热议问题