When you want to convert a DateTime to a string, use ToString(). By default, this converts the DateTime to a string using the current culture’s date/time format (from the OS). In most cases, you’ll want to specify the format to use. You can do this by passing in a format string consisting of format specifier characters. Here’s some examples of using common DateTime formats:
var dateOnly = DateTime.Now.ToString("yyyy-MM-dd");
Console.WriteLine(dateOnly);
//Outputs: 2023-05-04
var dateAndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(dateAndTime);
//Outputs: 2023-05-04 14:01:56
var timeOnly = DateTime.Now.ToString("HH:mm:ss.fff");
Console.WriteLine(timeOnly);
//Outputs: 14:01:56.745
var iso8601 = DateTime.Now.ToString("o");
Console.WriteLine(iso8601);
//Outputs: 2023-05-04T14:01:56.7453576-04:00
Code language: C# (cs)
DateTime has two main types of format strings: custom and standard (culture-specific and ISO-8601). I’ll show several examples for both types below.
Note: If the format string is invalid, it throws FormatException.
Custom DateTime format strings
You can create custom format strings by combining multiple format specifier characters. The following table shows the common format specifier characters organized by Date/Time part:
Date/Time Part | Format specifier characters |
Month | M = month (1-12) MM = month (01-12) MMM = month name abbreviated (Jan – Dec) MMMM = month name full (January – December) |
Day | d = day (1-31) dd = day (01-31) ddd = day of week abbreviated (Mon – Sun) dddd = day of week full (Monday – Sunday) |
Year | yy = year (ex: 23) yyyy = year (ex: 2023) |
Hour | h = 12-hour clock (1-12) hh = 12-hour clock (01-12) H = 24-hour clock (0-23) HH = 24-hour clock (00-23) |
Minute | m = minute (0-59) mm = minute (00-59) |
Second | s = second (1-59) ss = second (00-59) |
Milliseconds | fff (0.000-0.999 seconds). fffffff (0.0000000-0.9999999 seconds) Note: Use between one and seven ‘f’ characters. |
AM/PM | tt |
UTC offset | K (ex: -04:00) |
Warning: When you pass in an individual character as the format string (such as just “d”), it’s interpreted as a standard format. This can produce unexpected results or a FormatException.
You create custom format strings by combining the format specifier characters. The following table shows a few example custom format strings:
Format string | Example |
“yyyy-MM-dd” | 2023-05-04 |
“MMyy” | 0523 |
“M/d/yyyy” | 5/4/2023 |
“HH:mm” | 14:01 |
“HH:mm:ss.fff” | 14:01:56.745 |
“h:mm tt” | 2:01 PM |
“yyyy-MM-dd HH:mm” | 2023-05-04 14:01 |
“yyyy-MM-dd HH:mm K” | 2023-05-04 14:01 -0:400 |
“yyyy-MM-ddTHH:mm:ss.fffffffK” | 2023-05-04T14:01:56.7453576-04:00 |
“yyyyMMddHHmmss” | 20230504140156 |
“MMMM d (dddd)” | May 4 (Thursday) |
Tip: You can use the same format strings for parsing a DateTime from a string.
Standard DateTime format strings
The standard format strings consist of single characters. All of these are culture-specific (except for ISO-8601). This means the date/time format changes based on the culture you’re using. For example, when you want to use the “short date” standard format, you’re actually using “this culture’s short date format.” This can trip people up (so be sure to test that you can handle other culture’s date formats). If you need a specific format, stick with a custom format string (as shown in the above section).
The following table shows (most of) the standard DateTime format strings (including examples from two cultures):
Format | Description | Examples |
“o” | ISO-8601 | 2023-05-04T17:14:02.5904455-04:00 |
“d” | Short date (culture-specific) | en-US = 5/4/2023 fr-FR = 04/05/2023 |
“D” | Long date (culture-specific) | en-US = Thursday, May 4, 2023 fr-FR = jeudi 4 mai 2023 |
“t” | Short time (culture-specific) | en-US = 5:14 PM fr-FR = 17:14 |
“T” | Long time (culture-specific) | en-US = 5:14:02 PM fr-FR = 17:14:02 |
“G” | Date & Time (culture-specific) | en-US = 5/4/2023 5:14:02 PM fr-FR = 04/05/2023 17:14:02 |
“F” | Long Date & Time (culture-specific) | en-US = Thursday, May 4, 2023 5:14:02 PM fr-FR = jeudi 4 mai 2023 17:14:02 |
“Y” | Year & Month (culture-specific) | en-US = May 2023 fr-FR = mai 2023 |
Use a different culture with DateTime.ToString()
By default, DateTime.ToString() uses the current culture. You change specify the exact culture to use by passing in a CultureInfo, like this:
var franceInfo = CultureInfo.GetCultureInfo("fr-FR");
var franceYearMonth = DateTime.Now.ToString("Y", franceInfo);
Console.WriteLine(franceYearMonth); //Outputs: mai 2023
Code language: C# (cs)