Working with System.DateTime – Date and Time String Formatting with C#
Software Development with C# needs to work with dates and times. To work efficiently with Date and Time .NET has the System.DateTime (DateTime) namespace to help us. You can use DateTime to get DateTime values, generate new DateTime values and format DateTime values (parse strings into DateTime or DateTime in the string format).
Creating New DateTime Objects with C#
The default value of DateTime is Jan 1, 0001 at 12:00 midnight.
DateTime newDate = new DateTime();
Console.WriteLine("date: {0}", newDate); //date: 1/1/0001 12:00:00 AM
Creating New DateTime through the constructor
You can initialize the DateTime through the constructor using several pre-define options.
System.DateTime newDate = new System.DateTime(2010, 9, 5, 21, 35, 15, 777);
Console.WriteLine("date: {0}", newDate);//date: 9/5/2010 9:35:15 PM
If you need the current date and time? DateTime.Now will do it for you.
System.DateTime newDate = System.DateTime.Now;
Console.WriteLine("date: {0}", newDate);// Current Date Time will be display
Date and Time String Formatting with C#
The date and time value can be represented by either a DateTime or a DateTimeOffset value. Date and Time String Formatting fall into two categories:
- Standard Date and Time String Formatting
- Custom Date and Time String Formatting
Standard Date and Time String Formatting
Standard date and time String Formatting consist of one standard date and time format specifier. Each standard format specifier denotes a particular, commonly used string representation of a date and time. Any date and time String Formatting that contains more than one character, including white space, colon (:), forward slash is interpreted as a custom date and time format string.
We are going to format DateTime using DateTime.ToString method. The following table describes the standard date and time format specifiers.
| Table 1 – Standard Date and Time String Format Specifier | |||
| Format specifier | Description | Pattern value (for en-US culture) | Examples |
| "d" | Short date pattern. | M/d/yyyy | 8/15/2010 |
| "D" | Long date pattern. | dddd, MMMM dd, yyyy | Sunday, August 15, 2010 |
| "f" | Full date/time pattern (long time). | dddd, MMMM dd, yyyy h:mm tt | Sunday, August 15, 2010 1:45 PM |
| "F" | Full date/time pattern (long time). | dddd, MMMM dd, yyyy h:mm:ss tt | Sunday, August 15, 2010 1:45:30 PM |
| "g" | General date/time pattern (short time). | M/d/yyyy h:mm tt | 8/15/2010 1:45 PM |
| "G" | General date/time pattern (long time). | M/d/yyyy h:mm:ss tt | 8/15/2010 1:45:30 PM |
| "M", "m" | Month/day pattern. | MMMM dd | August 15 |
| "O", "o" | Round-trip date/time pattern. | 2010-08-15T13:45:30.0900000 | |
| "R", "r" | RFC1123 pattern. | ddd, dd MMM yyyy HH’:'mm’:'ss ‘GMT’ (*) | Sun, 15 Aug 2010 20:45:30 GMT |
| "s" | Sortable date/time pattern. | yyyy’-'MM’-'dd’T'HH’:'mm’:'ss (*) | 2010-08-15T13:45:30 |
| "t" | Short time pattern. | h:mm tt | 1:45 PM |
| "T" | Long time pattern. | h:mm:ss tt | 1:45:30 PM |
| "u" | Universal sortable date/time pattern. | yyyy’-'MM’-'dd HH’:'mm’:'ss’Z’ (*) | 2010-08-15 20:45:30Z |
| "U" | Universal full date/time pattern. | dddd, MMMM dd, yyyy h:mm:ss tt | Sunday, August 15, 2010 8:45:30 PM |
| "Y", "y" | Year month pattern. | MMMM, yyyy | August, 2010 |
Note: With Standard Date and Time String Formatting, the string representations of date and time values typically vary by culture. For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".
Custom Date and Time String Formatting
A custom format string consists of one or more custom date and time format specifiers. Any string that is not a standard date and time format string is interpreted as a custom date and time format string.
There are following custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).
The following table describes the custom date and time format specifiers and displays a result string produced by each format specifier.
| Table 2 – Custom Date and Time String Format Specifier | ||
| Format specifier | Description | Examples |
| "M/d/yyyy" | Short date pattern. | 8/15/2010 |
| "dddd, MMMM dd, yyyy" | Long date pattern. | Sunday, August 15, 2010 |
| "dddd, MMMM dd, yyyy h:mm tt" | Full date/time pattern (long time). | Sunday, August 15, 2010 1:45 PM |
| "dddd, MMMM dd, yyyy h:mm:ss tt" | Full date/time pattern (long time). | Sunday, August 15, 2010 1:45:30 PM |
| "M/d/yyyy h:mm tt" | General date/time pattern (short time). | 8/15/2010 1:45 PM |
| "M/d/yyyy h:mm:ss tt" | General date/time pattern (long time). | 8/15/2010 1:45:30 PM |
| "MMMM dd" | Month/day pattern. | August 15 |
| "ddd, dd MMM yyyy HH:mm:ss GMT" | RFC1123 pattern. | Sun, 15 Aug 2010 20:45:30 GMT |
| "yyyy-MM-ddTHH:mm:ss" | Sortable date/time pattern. | 2010-08-15T13:45:30 |
| "h:mm tt" | Short time pattern. | 1:45 PM |
| "h:mm:ss tt" | Long time pattern. | 1:45:30 PM |
| "yyyy-MM-dd HH:mm:ssZ" | Universal sortable date/time pattern. | 2010-08-15 20:45:30Z |
| "dddd, MMMM dd, yyyy h:mm:ss tt" | Universal full date/time pattern. | Sunday, August 15, 2010 8:45:30 PM |
| "MMMM, yyyy" | Year month pattern. | August, 2010 |
Date and Time String Formatting using String.Format method
We can also format DateTime using String.Format method, For example:
System.DateTime newDate = new System.DateTime(2010, 9, 5, 21, 35, 15, 777);
Console.WriteLine("date: {0}", newDate);//date: 9/5/2010 9:35:15 PM
Console.WriteLine(String.Format("{0:y yy yyy yyyy}", newDate)); // "10 10 2010 2010" year
Console.WriteLine(String.Format("{0:M MM MMM MMMM}", newDate)); // "9 09 Sep September" month
Console.WriteLine(String.Format("{0:d dd ddd dddd}", newDate)); // "9 09 Sun Sunday" day
Console.WriteLine(String.Format("{0:h hh H HH}", newDate)); // "9 09 21 21" hour 12/24
Console.WriteLine(String.Format("{0:m mm}", newDate)); // "35 35" minute
Console.WriteLine(String.Format("{0:s ss}", newDate)); // "15 15" second





You can also format by using Culture class.