Thursday, July 12, 2012

How to Use Date and Time in C# With Example?

Working With Date in C# Example:

 We can use DateTime Class in C# to work with date and time. From that we can get today's date. If we want yesterday's date we can add -1 to current date. If we want tomorrow's date we can simply add 1 to the current date. The following example illustrates how to work with date and time in c#.

Example Code:
  
class DateAndTimeExample
{
static void Main(string[] args)
{
   /*----------Working With Date--------------------*/
       
DateTime value = new DateTime(2012,2, 25);
Console.WriteLine("Date With Our Values:"+value); //It will print date with our values
Console.WriteLine("Today's Date:" + DateTime.Today);
Console.WriteLine("Tomorrow Date:" + DateTime.Today.AddDays(1));
Console.WriteLine("Yesterday Date:" + DateTime.Today.AddDays(-1));
}
}
Output: