How to get yesterday's date in C#

C#asp.net

C# Problem Overview


I want to retrieve yesterday's date in my ASP.NET web application using C#. I've tried searching for a solution but have not had much success. The code I'm using just outputs today's date:

string yr = DateTime.Today.Year.ToString();
string mn = DateTime.Today.Month.ToString();
string dt = DateTime.Today.Day.ToString();
date = string.Format("{0}-{1}-{2}", yr, mn, dt);

How can I get yesterday's date?

C# Solutions


Solution 1 - C#

Use DateTime.AddDays() method with value of -1

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

That will give you : {6/28/2012 12:00:00 AM}

You can also use

DateTime.Now.AddDays(-1)

That will give you previous date with the current time e.g. {6/28/2012 10:30:32 AM}

Solution 2 - C#

The code you posted is wrong.

You shouldn't make multiple calls to DateTime.Today. If you happen to run that code just as the date changes you could get completely wrong results. For example if you ran it on December 31st 2011 you might get "2011-1-1".

Use a single call to DateTime.Today then use ToString with an appropriate format string to format the date as you desire.

string result = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");

Solution 3 - C#

You will get yesterday date by this following code snippet.

DateTime dtYesterday = DateTime.Now.Date.AddDays(-1);

Solution 4 - C#

You don't need to call DateTime.Today multiple times, just use it single time and format the date object in your desire format.. like that

 string result = DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");

OR

 string result = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");

Solution 5 - C#

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

Solution 6 - C#

Something like this should work

var yesterday = DateTime.Now.Date.AddDays(-1);

DateTime.Now gives you the current date and time.

If your looking to remove the the time element then adding .Date constrains it to the date only ie time is 00:00:00.

Finally .AddDays(-1) removes 1 day to give you yesterday.

Solution 7 - C#

string result = DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");

Solution 8 - C#

DateTime dateTime = DateTime.Now ; 
string today = dateTime.DayOfWeek.ToString();
string yesterday = dateTime.AddDays(-1).DayOfWeek.ToString(); //Fetch day i.e. Mon, Tues
string result = dateTime.AddDays(-1).ToString("yyyy-MM-dd");

The above snippet will work. It is also advisable to make single instance of DateTime.Now;

Solution 9 - C#

DateTime.Today as it implies is todays date and you need to get the Date a day before so you subtract one day using AddDays(-1);

There are sufficient options available in DateTime to get the formatting like ToShortDateString depending on your culture and you have no need to concatenate them individually.

Also you can have a desirable format in the .ToString() version of the DateTime instance

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
QuestionakhilView Question on Stackoverflow
Solution 1 - C#HabibView Answer on Stackoverflow
Solution 2 - C#Mark ByersView Answer on Stackoverflow
Solution 3 - C#Hitesh PatelView Answer on Stackoverflow
Solution 4 - C#TalhaView Answer on Stackoverflow
Solution 5 - C#moribvndvsView Answer on Stackoverflow
Solution 6 - C#Chris MoutrayView Answer on Stackoverflow
Solution 7 - C#kavinhuhView Answer on Stackoverflow
Solution 8 - C#NIshankView Answer on Stackoverflow
Solution 9 - C#V4VendettaView Answer on Stackoverflow