how get yesterday and tomorrow datetime in c#

后端 未结 9 1241
Happy的楠姐
Happy的楠姐 2020-12-13 08:17

I have a code:

int MonthNow = System.DateTime.Now.Month;
int YearNow = System.DateTime.Now.Year;
int DayNow = System.DateTime.Now.Day;

How

9条回答
  •  囚心锁ツ
    2020-12-13 09:19

    You should do it this way, if you want to get yesterday and tomorrow at 00:00:00 time:

    DateTime yesterday = DateTime.Today.AddDays(-1);
    DateTime tomorrow = DateTime.Today.AddDays(1); // Output example: 6. 02. 2016 00:00:00
    

    Just bare in mind that if you do it this way:

    DateTime yesterday = DateTime.Now.AddDays(-1);
    DateTime tomorrow = DateTime.Now.AddDays(1); // Output example: 6. 02. 2016 18:09:23
    

    then you will get the current time minus one day, and not yesterday at 00:00:00 time.

提交回复
热议问题