Introduction
In this article, we perform various operations on dates and times using DateTime. A DateTime structure represents dates and times. We use DateTime to work with dates, times, and both. The DateTime type in the C# language provides useful methods and properties for computing these values. DateTime represents a date and time of the day. For example, there are methods to find important days, such as yesterday, tomorrow, the first of the year, and the last day.
1. To find today's date
Console.WriteLine("Today: {0}", DateTime.Today);
Console.WriteLine("Now: {0}", DateTime.Now);
Difference between Today and Now property
- Today: This displays today's date. The time value is 12:00:00.
- Now: This displays the current date and time of the system, expressed as the local time. In other words, the Now property returns a DateTime object that has the date and time values for right now.
2. To find yesterday's date
using System;
class Program
{
static DateTime YesterdayDate()
{
return DateTime.Today.AddDays(-1);
}
static void Main(string[] args)
{
DateTime yesterday = YesterdayDate();
Console.WriteLine("Yesterday was: {0}", yesterday);
}
}
3. To find tomorrow's date
using System;
class Program
{
static DateTime TomorrowDate()
{
return DateTime.Today.AddDays(1);
}
static void Main(string[] args)
{
DateTime tomorrow = TomorrowDate();
Console.WriteLine("Tomorrow date is: {0}", tomorrow);
}
}
4. To find the First day in the year
using System;
class Program
{
static DateTime FirstDayOfYear()
{
return FirstDayOfYear(DateTime.Today);
}
static DateTime FirstDayOfYear(DateTime y)
{
return new DateTime(y.Year, 1, 1);
}
static void Main(string[] args)
{
DateTime m = new DateTime(1999, 6, 1);
Console.WriteLine("First day of 1999: {0}", FirstDayOfYear(m));
}
}
5. Adding DateTime values
DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");
TimeSpan addDuration = TimeSpan.Parse("1.01:01:01");
theDate = theDate.Add(addDuration);
6. Subtracting DateTime values
DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");
TimeSpan addDuration = TimeSpan.Parse("1.01:01:01");
theDate = theDate.Subtract(addDuration);
Console.WriteLine("Subtracted date is: {0}", theDate);
Other DateTime Properties
7. DayOfWeek
// Create a DateTime object for June 21, 2011, at 7:10:24 AM
DateTime findwyd = new DateTime(2011, 6, 21, 7, 10, 24);
// Retrieve and print the day of the week
Console.WriteLine("Day of Week: {0}", findwyd.DayOfWeek);
// Retrieve and print the day of the year
Console.WriteLine("Day of Year: {0}", findwyd.DayOfYear);
// Retrieve and print the time of day
Console.WriteLine("Time of Day: {0}", findwyd.TimeOfDay);
8. DayOfYear
// Create a DateTime object for June 21, 2011, at 7:10:24 AM
DateTime findwyd = new DateTime(2011, 6, 21, 7, 10, 24);
// Retrieve and print the day of the year
Console.WriteLine("Day of Year: {0}", findwyd.DayOfYear);
9. TimeOfDay
The TimeOfDay property returns the time element from a DateTime.
// Create a DateTime object for June 21, 2011, at 7:10:24 AM
DateTime findwyd = new DateTime(2011, 6, 21, 7, 10, 24);
// Retrieve and print the time of day
Console.WriteLine("Time of Day: {0}", findwyd.TimeOfDay);
Example
using System;
namespace datetime_function
{
class Program
{
static DateTime Yesterdaydate()
{
return DateTime.Today.AddDays(-1);
}
static DateTime Tomorrowdate()
{
return DateTime.Today.AddDays(1);
}
static DateTime FirstDayOfYear()
{
return FirstDayOfYear(DateTime.Today);
}
static DateTime FirstDayOfYear(DateTime y)
{
return new DateTime(y.Year, 1, 1);
}
static void Main(string[] args)
{
DateTime findwyd = new DateTime(2011, 6, 21, 7, 10, 24);
Console.WriteLine("Today: {0}", DateTime.Today);
Console.WriteLine("Now: {0}", DateTime.Now);
DateTime yes = Yesterdaydate();
Console.WriteLine("Yesterday was: {0}", yes);
DateTime d = Tomorrowdate();
Console.WriteLine("Tomorrow date is: {0}", d);
DateTime m = new DateTime(1999, 6, 1);
Console.WriteLine("First day of 1999: {0}", FirstDayOfYear(m));
DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");
TimeSpan addduration = TimeSpan.Parse("1.01:01:01");
theDate = theDate.Add(addduration);
Console.WriteLine("Added date is: {0}", theDate);
theDate = theDate.Subtract(addduration);
Console.WriteLine("Subtracted date is: {0}", theDate);
Console.WriteLine("Day of Week: {0}", findwyd.DayOfWeek);
Console.WriteLine("Day of Year: {0}", findwyd.DayOfYear);
Console.WriteLine("Time of Day: {0}", findwyd.TimeOfDay);
}
}
}
Output