how get yesterday and tomorrow datetime in c#

C#Datetime

C# Problem Overview


I have a code:

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

How can I get yesterday and tomorrow day, month and year in C#?

Of course, I can just write:

DayTommorow = DayNow +1;

but it may happen that tomorrow is other month or year. Are there in C# built-in tools to find out yesterday and today?

C# Solutions


Solution 1 - C#

DateTime tomorrow = DateTime.Today.AddDays(1);
DateTime yesterday = DateTime.Today.AddDays(-1);

Solution 2 - C#

You can find this info right in the API reference.

var today = DateTime.Today;
var tomorrow = today.AddDays(1);
var yesterday = today.AddDays(-1);

Solution 3 - C#

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.

Solution 4 - C#

Today :

DateTime.Today

Tomorrow :

DateTime.Today.AddDays(1)

Yesterday :

DateTime.Today.AddDays(-1)

Solution 5 - C#

You want DateTime.Today.AddDays(1).

Solution 6 - C#

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

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

Solution 7 - C#

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.

Solution 8 - C#

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);

Solution 9 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexander V.View Question on Stackoverflow
Solution 1 - C#Erik LarssonView Answer on Stackoverflow
Solution 2 - C#TabrezView Answer on Stackoverflow
Solution 3 - C#Jo SmoView Answer on Stackoverflow
Solution 4 - C#alerootView Answer on Stackoverflow
Solution 5 - C#SLaksView Answer on Stackoverflow
Solution 6 - C#NovakovView Answer on Stackoverflow
Solution 7 - C#paulsm4View Answer on Stackoverflow
Solution 8 - C#Namig HajiyevView Answer on Stackoverflow
Solution 9 - C#Martin DyeView Answer on Stackoverflow