C#.net Get Yesterday.

C#.net Get Yesterday.

You can get yesterday and today using DateTime add days method. Here we will see how to get today and yesterday's date in [C#.net] and [Vb.net].

Program to get yesterday in [C#].

C# DateTime
using System;
namespace DateAndTime.Examples
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Today is {0}", DateTime.Today);
DateTime yesterday  = GetYesterdayDate();
Console.WriteLine("Yesterday is: {0}", yesterday);
Console.ReadLine();
}
static DateTime GetYesterdayDate()
{      
return DateTime.Today.AddDays(-1);
}
}
}
VB.net Program

(VB.net) example to get yesterdays day.

Namespace DateAndTime.Examples
Class Program
Private Shared Sub Main(args As String())
Console.WriteLine("Today is {0}", DateTime.Today)
Dim yesterday As DateTime = GetYesterdayDate()
Console.WriteLine("Yesterday is: {0}", yesterday)
Console.ReadLine()
End Sub
Private Shared Function GetYesterdayDate() As DateTime
Return DateTime.Today.AddDays(-1)
End Function
End Class
End Namespace
Output.
Today is 1/1/2012 12:00:00 AM.
Yesterday is: 12/31/2011 12:00:00 AM.
Description.

Example shows today and yesterdays date and time. DateTime.Today gives today's date. GetYesterdayDate() function returns yesterdays date. AddDays method accepts number of day's to add in given date.

DateTime examples

How to format Time in C#.net and VB.net program.
DateTime Time Format string {HH:mm:ss}.
C#.net datetime Culture.
C#.net DateTime Month.
string was not recognised as a valid datetime
Get Today
DateTime in English UK format
 Get Day Of year

Tags:

C#.net get tomorrows dateJquery Get Target Of EventGetfilename GetExtensionHow to get Jquery selected optionC# Regular Expression Get Text Between BracketsC# Get File SizeHow are you using session variables in Asp.net with C#.netJQuery Get Element By ID or class name or tag nameC# Get Current Directory of ExecutableIFormatProvider DateTime C#.netJQuery Get .next() Element

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.