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#].
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) 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
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5