C# DateTime class in C# provides properties and methods to format dates in different DateTime formats. This article explains how to work with the C# DateTime format Including Timer and Sleep Methods.
Lets Starts with C# DateTime Object–
DateTime is a Struct in the System namespace. It helps the developers to retrieve information about system date, time, month, year, or even the day of the week.
How To Create The C# DateTime Object?
class Program
{
static void Main(string[] args)
{
// Year, Month, Date
DateTime dt = new DateTime(2016, 03, 07);
Console.WriteLine(dt.ToString());
Console.Read();
}
}
The output of the above program will be: 3/7/2016 12:00:00 AM
If you see here in this output time is shown as 12:00:00 AM (default time). We haven’t set any time value and thus the system automatically assigned default values to an hour, minute, and second.
DateTime Object Properties:
DateTime dt = new DateTime(2016, 03, 07);
int year = dt.Year;
int month = dt.Month;
int date = dt.Day;
int hour = dt.Hour;
int min = dt.Minute;
int sec = dt.Second;
int dayOfWeek = Convert.ToInt32(dt.DayOfWeek);
int dayOfYear = dt.DayOfYear;
Year
Year property retrieves the year component of the date represented by the date-time instance. It returns an integer value expressed as a value between 1 and 9999.
Month
Month property retrieves the month component of the date represented by the date-time instance. It returns an integer value expressed as a value between 1 and 12.
Day
Day property retrieves the date component of the month represented by the date-time instance. It returns an integer value expressed as a value between 1 and 31.
Hour
Hour property retrieves the hour component of the date represented by the date-time instance. It returns an integer value expressed as a value between 0 and 23.
Minute
Min property retrieves the minute component of the date represented by the date-time instance. It returns an integer value expressed as a value between 0 and 59.
Second
Second property retrieves the second component of the date represented by the date-time instance. It returns an integer value expressed as a value between 0 and 59.
Day of the Week
Day of the week property retrieves the enumerated constant that indicates the day of the week represented by the date-time object. It also requires casting to accept integer value.
Day of Year
Day of year property retrieves the day of the year represented by the date-time object. It returns an integer value expressed as a value between 0 and 366.
Now Lets have a look at below code to see how we can use these properties.
DateTime dt = new DateTime(2016, 03, 07);
int year = dt.Year;
int month = dt.Month;
int date = dt.Day;
int hour = dt.Hour;
int min = dt.Minute;
int sec = dt.Second;
int dayOfWeek = Convert.ToInt32(dt.DayOfWeek);
int dayOfYear = dt.DayOfYear;
Console.WriteLine(year);
Console.WriteLine(month);
Console.WriteLine(date);
Console.WriteLine(hour);
Console.WriteLine(min);
Console.WriteLine(sec);
Console.WriteLine(dayOfWeek);
Console.WriteLine(dayOfYear);
Console.Read();
The output of the above program will be:
Year: 2016
Month : 3
Date: 7
Hour : 0
Minute : 0
Second : 0
Day of the week: 1
Day of the year: 67
How To Get Current Date Time?
DateTime object contains a number of different methods to access system time. The “Now” method allows you to get the current system time/date and even allows you to operate on it.
Syntax:
DateTime dt = DateTime.Now;
We can easily convert it to string to get the current date-time or we can even change the format of the date by using the below formats.
NOTE : Consider current date time is 7 March 2016 11:06:25
Display date time in 24 hour format
DateTime.Now.ToString(“dd/MM/yyyy HH:mm:ss”);
Ans :07/03/2016 11:06:25
Note: HH is used for 24 hour
format.
hh for 12 hour format
Display am/pm in date time or 12 hour format time.
DateTime.Now.ToString(“hh:mm:ss tt”);
Ans:11:06:25 AM
Note: “tt”
for AM or PM
Display time zone in date time.
DateTime.Now.ToString(“hh:mm:ss tt zzz”);
Ans: 11:06:25 AM +05:30 (Here +05:30 is time zone for India)
Note: zzz is used to
display time zone.
Show full month name in date.
DateTime.Now.ToString(“dd-MMMM-yy”);
Ans: 07-March-16
How to format full day name in date?
DateTime.Now.ToString(“dddd-MMMM-yy”);
Ans: Monday-March-16
Note: MMMM for full month
name
How to format full year name in date?
DateTime.Now.ToString (“dddd-MMMM-yyyy”);
Ans: Monday-March-2016
Note: yyyy for four digit
year
To reduce/subtract date or days
DateTime.Now.AddDays(-10)
To add days
DateTime.Now.AddDays(10)
C# DateTime to “YYYYMMDDHHMMSS” format
DateTime.Now.ToString (“yyyyMMddHHmmss”);
You can convert the dateTime to any DateTime format by simply adding the required format in ToString (.ToString(“yyyyMMdd”)) as shown in the above example. Please note that C# datetime format is case-sensitive. Please check below.
y = year, m = minutes / M = months, d= date, h = 12 hour, H = 24 hour, s= seconds
Let’s have a look at below C# DateTime formats and their outputs. Here we see all the patterns of the C# DateTime, format, and outputs.
C# DateTime Format | Output |
DateTime.Now.ToString(“MM/dd/yyyy HH:mm”) | 11/29/2019 09:11 |
DateTime.Now.ToString(“MM/dd/yyyy hh:mm tt”) | 11/29/2019 09:11 AM |
DateTime.Now.ToString(“MM/dd/yyyy H:mm”) | 11/29/2019 9:11 |
DateTime.Now.ToString(“MM/dd/yyyy h:mm tt”) | 11/29/2019 9:11 AM |
DateTime.Now.ToString(“MM/dd/yyyy HH:mm:ss”) | 11/29/2019 9:11:32 |
DateTime.Now.ToString(“MM/dd/yyyy”) | 11/29/2019 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy”) | Friday, 29 November 2019 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy HH:mm”) | Friday, 29 November 2019 09:11 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy hh:mm tt”) | Friday, 29 November 2019 09:11 AM |
DateTime.Now.ToString(“dddd, dd MMMM yyyy h:mm”) | Friday, 29 November 2019 9:11 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy h:mm tt”) | Friday, 29 November 2019 9:11 AM |
DateTime.Now.ToString(“dddd, dd MMMM yyyy HH:mm:ss”) | Friday, 29 November 2019 09:11:32 |
DateTime.Now.ToString(“MMMM dd”) | November 29 |
DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK”) | 2019-11-29T09:11:32.0839641+05:30 |
DateTime.Now.ToString(“ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT'”) | Fri, 16 Nov 2019 09:11:32 GMT |
DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss”) | 2019-11-29T09:11:32 |
DateTime.Now.ToString(“HH:mm”) | 09:11 |
DateTime.Now.ToString(“hh:mm tt”) | 09:11 AM |
DateTime.Now.ToString(“H:mm”) | 9:11 |
DateTime.Now.ToString(“h:mm tt”) | 9:11 AM |
DateTime.Now.ToString(“HH:mm:ss”) | 09:11:32 |
DateTime.Now.ToString(“yyyy MMMM”) | 2019 November |
DateTime.Now.ToString(“yyyy MMM”) | 2019 Nov |
DateTime.Now.ToString(“MM.dd.yyyy HH:mm”) | 11.29.2019 09:11 |
DateTime.Now.ToString(“MM.dd.yyyy hh:mm tt”) | 11.29.2019 09:11 AM |
DateTime.Now.ToString(“MM-dd-yyyy H:mm”) | 11-29-2019 9:11 |
DateTime.Now.ToString(“MM-dd-yyyy h:mm tt”) | 11-29-2019 9:11 AM |
DateTime.Now.ToString(“MM.dd.yyyy HH:mm:ss”) | 11.29.2019 9:11:32 |
DateTime.Now.ToString(“dd/MM/yyyy”) | 29/11/2019 |
Lets have a look at below sample C# code that uses these C# datetime formats.
namespace DateTimeFormat { class Program { static void Main(string [] args) { // dt erpresents DateTime object. // Here DateTime is a Struct in the System namespace. //year,month,date,hour,minute,seconds DateTime dt = new DateTime(2016, 03, 07, 11, 06, 25); Console.WriteLine("Date: " + dt.ToString()); // Datetime in different formats Console.WriteLine("***Datetime in different formats***"); Console.WriteLine(dt.ToString("dd/MM/yyyy")); Console.WriteLine(dt.ToString("dd-MM-yyyy")); Console.WriteLine(dt.ToString("MM/dd/yyyy")); Console.WriteLine(dt.ToString("MM-dd-yyyy")); Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm:ss")); Console.WriteLine(dt.ToString("yyyyMMddHHmmss")); Console.WriteLine(dt.ToString("HH:mm")); Console.WriteLine(dt.ToString("dd/MM/yyyy HH:mm")); Console.WriteLine(dt.ToString("HH:mm:ss")); Console.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss")); Console.WriteLine(dt.ToString("hh:mm tt")); Console.WriteLine(dt.ToString("dd/MM/yyyy hh:mm tt")); Console.WriteLine(dt.ToString("H:mm")); Console.WriteLine(dt.ToString("dd/MM/yyyy H:mm")); Console.WriteLine(dt.ToString("h:mm tt")); Console.WriteLine(dt.ToString("MM/dd/yyyy h:mm tt")); Console.WriteLine(dt.ToString("MMMM dd")); Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss.fffffffK")); Console.WriteLine(dt.ToString("ddd, dd MMM yyy HH:mm:ss 'GMT'")); Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss")); Console.WriteLine(dt.ToString("yyyy MMMM")); Console.ReadLine(); } } }
The output of the above program will be something like this:
The following table describes various C# DateTime formats and their results. Here we see all the patterns of the C# DateTime format, and results.
For | Format | Result | Notes |
---|---|---|---|
Day | %d | 7 | Represents the day of the month as a number tom 1 through 31. |
dd | 07 | Represents the day of the month as a number from 01 through 31. | |
ddd | Mon | Represents the abbreviated name of the day (Mon Tues etc.). | |
dddd | Monday | Represents the full name of the day (Monday, Tuesday, etc.) | |
Year | %y | 11 | Represents the year as a number from 1 through 99. |
yy | 11 | Represents the year as a number from 01 through 99. | |
yyyy | 2016 | Represents the year (2011, 2012, etc.) | |
Month | %M | 3 | Represents the Month number (e.g. 3, 4, etc.) |
MM | 03 | Represents the Month number (e.g. 03, 04, etc.) | |
MMM | Mar | Represents the abbreviated Month name (e.g. Mar, Apr, etc.) | |
MMMM | March | Represents the full Month name (e.g. March, April, etc.) | |
Hour (8 PM) | %h | 8 | Represents the 12-hour clock hour (e.g. 8, 9, etc.) |
hh | 08 | Represents the 12-hour clock, with leading 0 (e.g. 08, 09, etc.) | |
HH | 20 | Represents the 24-hour clock hour, with leading 0 (e.g. 22) | |
Minutes | %m | 6 | Minutes |
mm | 06 | Minutes with leading zero | |
Seconds | s | 25 | Seconds |
ss | 25 | Seconds with leading zero | |
AM PM | tt | AM | AM/PM |
TimeZone | zzz | +5:30 | Represents the time zone (e.g. +05.30) |
What Is Sleep Method?
The sleep method is used to pause the running thread for a specific time span. It accepts time in milliseconds. Sleep is very useful in a multi-threading environment where you want one thread to stop to make way for other threads to complete their execution.
Syntax
System.Threading.Thread.Sleep(1000);
Conclusion
In this article, we have learned about DateTime class. A DateTime object is used to gather information about the date and time of the system or to set a custom date and time for use for a particular application requirement.
Date and Time in C# are handled by DateTime class in C# that provides properties and methods to format dates in different C# DateTime formats.
We also learned about Sleep method, it is the part of System.Threading and is used to pause the execution for a certain time interval. This allows the programmers to start another thread in the multi-threading environment while the previous thread is paused.