C# DateTime Format yyyy-mm-dd
Working with dates and times is a common task in programming, and C# provides a robust DateTime structure to handle these scenarios. When you need to display or manipulate DateTime values as strings, it’s essential to understand how to format them. In C#, you can achieve this through custom formatting or by using standard format strings.
DateTime Format Examples:
Format | Result |
---|---|
DateTime.Now.ToString("t") | 4:05 PM |
DateTime.Now.ToString("d") | 3/9/2024 |
DateTime.Now.ToString("T") | 4:05:07 PM |
DateTime.Now.ToString("D") | Sunday, March 09, 2024 |
DateTime.Now.ToString("f") | Sunday, March 09, 2024 4:05 PM |
DateTime.Now.ToString("F") | Sunday, March 09, 2024 4:05:07 PM |
DateTime.Now.ToString("g") | 3/9/2024 4:05 PM |
DateTime.Now.ToString("G") | 3/9/2024 4:05:07 PM |
DateTime.Now.ToString("m") | March 09 |
DateTime.Now.ToString("y") | March, 2024 |
DateTime.Now.ToString("r") | Sun, 09 Mar 2024 16:05:07 GMT |
DateTime.Now.ToString("s") | 2024-03-09T16:05:07 |
DateTime.Now.ToString("u") | 2024-03-09 16:05:07Z |
dt.ToString("MM/dd/yyyy") | 05/29/2024 |
dt.ToString("dddd, dd MMMM yyyy") | Friday, 29 May 2024 |
dt.ToString("dddd, dd MMMM yyyy HH:mm") | Friday, 29 May 2024 05:50 |
dt.ToString("dddd, dd MMMM yyyy hh:mm tt") | Friday, 29 May 2024 05:50 AM |
dt.ToString("dddd, dd MMMM yyyy H:mm") | Friday, 29 May 2024 5:50 |
dt.ToString("dddd, dd MMMM yyyy h:mm tt") | Friday, 29 May 2024 5:50 AM |
dt.ToString("dddd, dd MMMM yyyy HH:mm:ss") | Friday, 15 May 2024 06:40:02 |
dt.ToString("MM/dd/yyyy HH:mm") | 05/29/2024 05:50 |
dt.ToString("MM/dd/yyyy hh:mm tt") | 05/29/2024 05:50 AM |
dt.ToString("MM/dd/yyyy H:mm") | 05/29/2024 5:50 |
dt.ToString("MM/dd/yyyy h:mm tt") | 05/29/2024 5:50 AM |
dt.ToString("MM/dd/yyyy HH:mm:ss") | 05/29/2024 05:50:06 |
dt.ToString("MMMM dd") | May 29 |
dt.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK") | 2024-05-16T05:50:06.7199222-04:00 |
dt.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’") | Fri, 16 May 2024 05:50:06 GMT |
dt.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss") | 2024-05-16T05:50:06 |
dt.ToString("HH:mm") | 05:50 |
dt.ToString("hh:mm tt") | 05:50 AM |
dt.ToString("H:mm") | 5:50 |
dt.ToString("h:mm tt") | 5:50 AM |
dt.ToString("HH:mm:ss") | 05:50:06 |
dt.ToString("yyyy MMMM") | 2024 May |
C# DateTime To String Format
Custom DateTime Formatting
Custom DateTime formatting in C# allows you to specify the exact format you want for displaying dates and times. This is achieved by using format specifiers that represent different components of the DateTime structure.
To use custom formatting, you can call the ToString method on a DateTime object and pass the desired format string as an argument. For example:
DateTime myDateTime = DateTime.Now;
string formattedDateTime = myDateTime.ToString("dddd, MMMM dd, yyyy HH:mm:ss");
Console.WriteLine(formattedDateTime);
//Output:
// Wednesday, March 02, 2024 15:30:45
In this example, the format string “dddd, MMMM dd, yyyy HH:mm:ss” represents the day of the week, full month name, day of the month, four-digit year, and time in 24-hour format.
// Date numbers
DateTime.Now.ToString("M/d/yyyy"); // "3/2/2024"
DateTime.Now.ToString("MM/dd/yyyy"); // "03/02/2024"
DateTime.Now.ToString("MM/dd/yy"); // "03/02/24"
// Date names
DateTime.Now.ToString("ddd, MMM d, yyyy"); // "Sat, Mar 2, 2024"
DateTime.Now.ToString("dddd, MMMM d, yyyy"); // "Saturday, March 2, 2024"
Standard DateTime Formatting
C# also provides standard DateTime formatting options through the ToString method. These options offer predefined formats for commonly used date and time representations.
To use standard formatting, you can again call the ToString method on a DateTime object and pass the desired standard format string:
DateTime myDateTime = DateTime.Now;
string standardFormattedDateTime = myDateTime.ToString("F");
Console.WriteLine(standardFormattedDateTime);
//Output:
// Wednesday, March 02, 2024 15:30:45
In this example, the standard format string “F” is used to represent the full date and long time pattern.
Here are some common format specifiers:
Specifier | DateTimeFormatInfo property | Pattern value |
---|---|---|
d | Short date pattern | M/d/yyyy (6/15/2023) |
D | Long date pattern | dddd, MMMM dd, yyyy (Monday, June 15, 2023) |
t | Short time pattern | h:mm tt (1:45 PM) |
T | Long time pattern | h:mm:ss tt (1:45:30 PM) |
f | Full date and short time pattern | dddd, MMMM dd, yyyy h:mm tt (Monday, June 15, 2023 1:45 PM) |
F | Full date and long time pattern | dddd, MMMM dd, yyyy h:mm:ss tt (Monday, June 15, 2023 1:45:30 PM) |
g | General short date and short time pattern | M/d/yyyy h:mm tt (6/15/2023 1:45 PM) |
G | General short date and long time pattern | M/d/yyyy h:mm:ss tt (6/15/2023 1:45:30 PM) |
m, M | Month and day pattern | MMMM dd (June 15) |
y, Y | Year and month pattern | MMMM, yyyy (June 2023) |
* s | Sortable date and time pattern | yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss |
* r, R | RFC1123 Pattern | ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ |
* u | Universal Sortable DateTime Pattern | yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ |
* Standard Format Specifiers in String.Format method.

Table of Contents
Reference: Microsoft.com