how get yesterday and tomorrow datetime in c#

后端 未结 9 1243
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:15

    To get "local" yesterday in UTC.

      var now = DateTime.Now;
      var yesterday = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Utc).AddDays(-1);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 09:20

    You can find this info right in the API reference.

    var today = DateTime.Today;
    var tomorrow = today.AddDays(1);
    var yesterday = today.AddDays(-1);
    
    0 讨论(0)
提交回复
热议问题