how get yesterday and tomorrow datetime in c#

后端 未结 9 1242
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 08:56
    DateTime tomorrow = DateTime.Today.AddDays(1);
    DateTime yesterday = DateTime.Today.AddDays(-1);
    
    0 讨论(0)
  • 2020-12-13 08:57

    Today :

    DateTime.Today

    Tomorrow :

    DateTime.Today.AddDays(1)
    

    Yesterday :

    DateTime.Today.AddDays(-1)
    
    0 讨论(0)
  • 2020-12-13 09:04

    The trick is to use "DateTime" to manipulate dates; only use integers and strings when you need a "final result" from the date.

    For example (pseudo code):

    1. Get "DateTime tomorrow = Now + 1"

    2. Determine date, day of week, day of month - whatever you want - of the resulting date.

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

    Use DateTime.AddDays() (MSDN Documentation DateTime.AddDays Method).

    DateTime tomorrow = DateTime.Now.AddDays(1);
    DateTime yesterday = DateTime.Now.AddDays(-1);
    
    0 讨论(0)
  • 2020-12-13 09:10

    Beware of adding an unwanted timezone to your results, especially if the date is going to be sent out via a Web API. Use UtcNow instead, to make it timezone-less.

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

    You want DateTime.Today.AddDays(1).

    0 讨论(0)
提交回复
热议问题