Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how get yesterday and tomorrow datetime in c#

Tags:

c#

datetime

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?

like image 398
Alexander V. Avatar asked Nov 20 '11 19:11

Alexander V.


People also ask

How to get yesterday DateTime c#?

Approach Ivar yestedaysDay = DateTime. Now. AddDays(-1); This will give you the exact day with a timestamp of yesterday (24 hours behind the current time).


2 Answers

DateTime tomorrow = DateTime.Today.AddDays(1); DateTime yesterday = DateTime.Today.AddDays(-1); 
like image 131
Erik Larsson Avatar answered Oct 01 '22 10:10

Erik Larsson


You can find this info right in the API reference.

var today = DateTime.Today; var tomorrow = today.AddDays(1); var yesterday = today.AddDays(-1); 
like image 38
Tabrez Avatar answered Oct 01 '22 08:10

Tabrez