The DateTime Type C# has built-in support for dates and times
This type can be used to represent a date and time, and it provides access to the system clock
The type also supports many operations that can make it easy to work with dates and times
The following block demonstrates some DateTime operations:
varcurrentTime=DateTime.Now;// current timevartoday=DateTime.Today;// current date (time is midnight)vartomorrow=DateTime.Today.AddDays(1);varyesterday=DateTime.Today.AddDays(-1);varsomeDate=newDateTime(2016,7,1);// 1 July 2016, midnightvarsomeMoment=newDateTime(2016,7,1,8,0,0);// 1 July 2016, 08:00.00varsomeDay=DateTime.Parse("7/1/2016");
The DateTime.Now and DateTime.Today properties are static, means you can access them without having an instance of a DateTime
Formatting Dates and Times
There are many ways to display dates and times. Times can be represented using 12-hour AM/PM style times, or using 24-hour format.
In formatting operations, custom date and time format strings can be used with the ToString method of a date and time instance.
The following example illustrates this:
Getting to Parts of Dates and Times
DateTime type has many properties you can use to access different individual components of the date or time, such as the month, day, year, hour, minute, etc. The following sample demonstrates some of these properties:
Calculating Durations Between DateTimes
Sometimes it can be useful to think about the difference between two times, or the duration of an event. For instance, you could calculate how many days are left in the year. To do so, you would need to determine the first day of the next year, and then compare that to today. The following code shows how to do this:
This produces a new type called a TimeSpan, which is used to represent a span of time, or a duration.
TimeSpan provides many ways to represent its value, the TotalDays property will show how many full days the TimeSpan includes.
Exercise
Write a program that accepts a birth date ( dd/MMM/yyyy) and calculates how many days old the person with that birth date is currently.
using System;
class Program
{
static void Main()
{
DateTime d1 = DateTime.Today;
Console.WriteLine("Today is " + d1.ToString("MMMM dd, yyyy"));
Console.WriteLine("Today is " + d1.ToString("dd/MMM/yyyy"));
Console.WriteLine("Today is " + d1.ToString("dd/MM/yy"));
Console.WriteLine("Today is " + d1.ToString("dd-MM-yy"));
Console.WriteLine("Today is " + d1.ToString("dd.MM.yy"));
}
}
var someTime = new DateTime(2016,7,1,11,10,9); // 1 July 2016 11:10:09 AM
int year = someTime.Year; // 2016
int month = someTime.Month; // 7
int day = someTime.Day; // 1
int hour = someTime.Hour; // 11
int minute = someTime.Minute; // 10
int second = someTime.Second; // 9
DateTime nextYear = new DateTime(DateTime.Today.Year+1, 1, 1);
TimeSpan duration = nextYear - DateTime.Today;
Console.WriteLine($"There are {duration.TotalDays} days left in the year");
Enter DOB in dd/MMM/yyyy format
07/sep/2010
4187
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter DOB in dd/MMM/yyyy format");
var dobString = Console.ReadLine();
//write your code here
}
}