C# DateTime with Examples

DateTime is commonly used in entire .Net applications where the language C# is used. Date and Time in C# are defined by the DateTime object.

Creating a Date Object

The Date class has two versions of the constructor which is used to create an instance. As said in our previous lectures, using a new keyword calls the constructor and creates an object.

DateTime dateTime = new DateTime();

The default and minimum value of DateTime are Jan 1, 0001 00:00:00 and the maximum value is December 31, 9999 11:59:59.

Examples:

//current date, month, year
DateTime dateTime = DateTime.Now;
Console.WriteLine("Year - "+ dateTime.Year); //current year
Console.WriteLine("Month - " + dateTime.Month); //current month
Console.WriteLine("Date - " + dateTime.Day); //current time
Console.WriteLine("Time - " + dateTime.TimeOfDay); //current Time
Console.WriteLine("Hour - " + dateTime.Hour); //hour
Console.WriteLine("Minutes - " + dateTime.Minute); //Minutes
Console.WriteLine("Seconds - " + dateTime.Second); // seconds

Output:

Year - 2021
Month - 1
Date - 12
Time - 16:25:38.3807854
Hour - 16
Minutes - 25
Seconds - 38

DateTime Format

In C# DateTime provide methods to format date. In the below example, you will find the basic format of DateTime.

Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy"));
Console.WriteLine(DateTime.Now.ToString("dddd, dd MMMM yyyy"));
Console.WriteLine(DateTime.Now.ToString("dddd, dd MMMM yyyy"));
Console.WriteLine(DateTime.Now.ToString("dddd, dd MMMM yyyy"));
Console.WriteLine(DateTime.Now.ToString("dddd, dd MMMM yyyy"));
Console.WriteLine(DateTime.Now.ToString("dddd, dd MMMM yyyy"));
Console.WriteLine(DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy HH:mm"));
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy hh:mm tt"));
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy H:mm"));
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy h:mm tt"));
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"));
Console.WriteLine(DateTime.Now.ToString("MMMM dd"));
Console.WriteLine(DateTime.Now.ToString("HH:mm"));
Console.WriteLine(DateTime.Now.ToString("hh:mm tt"));
Console.WriteLine(DateTime.Now.ToString("H:mm"));
Console.WriteLine(DateTime.Now.ToString("h:mm tt"));
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
Console.WriteLine(DateTime.Now.ToString("yyyy MMMM"));

Output:

01/12/2021
Tuesday, 12 January 2021
Tuesday, 12 January 2021
Tuesday, 12 January 2021
Tuesday, 12 January 2021
Tuesday, 12 January 2021
Tuesday, 12 January 2021 16:26:46
01/12/2021 16:26
01/12/2021 04:26 PM
01/12/2021 16:26
01/12/2021 4:26 PM
01/12/2021 16:26:46
January 12
16:26
04:26 PM
16:26
4:26 PM
16:26:46
2021 January

For more DateTime format, please check String Format for DateTime In C#.

Comparison of two dates

C# provides a method DateTime. Compare for the comparison of two dates let’s make some sense with the help of an example.

//first date
DateTime date1 = new DateTime(2021/01/04);
DateTime date2 = new DateTime(2021/01/05);
//C# compare method
int Difference = DateTime.Compare(date1, date2);            
Console.WriteLine("Difference - " + Difference);

Output:

Difference - 1

Conversion of DateTime

DateTime to String

//Date
DateTime date1 = new DateTime(2021/01/04);
//String
string convert_date = date1.ToString();

String to DateTime

//String
string convert_date = "2021/01/05";
DateTime dateTime = DateTime.Parse(convert_date.ToString());
Console.WriteLine(dateTime);

Format Conversion

//String
string convert_date = "2021/01/05";
DateTime dateTime = DateTime.Parse(convert_date.ToString());

Console.WriteLine(dateTime.ToString("MMMM/dd/yyyy"));