C# – Convert DateTime to string

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");
//Outputs: 2023-05-04

var dateAndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//Outputs: 2023-05-04 14:01:56

var timeOnly = DateTime.Now.ToString("HH:mm:ss.fff");
//Outputs: 14:01:56.745

var iso8601 = DateTime.Now.ToString("o");
//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 PartFormat specifier characters
MonthM = month (1-12)
MM = month (01-12)
MMM = month name abbreviated (Jan – Dec)
MMMM = month name full (January – December)
Dayd = day (1-31)
dd = day (01-31)
ddd = day of week abbreviated (Mon – Sun)
dddd = day of week full (Monday – Sunday)
Yearyy = year (ex: 23)
yyyy = year (ex: 2023)
Hourh = 12-hour clock (1-12)
hh = 12-hour clock (01-12)
H = 24-hour clock (0-23)
HH = 24-hour clock (00-23)
Minutem = minute (0-59)
mm = minute (00-59)
Seconds = second (1-59)
ss = second (00-59)
Millisecondsfff (0.000-0.999 seconds).
fffffff (0.0000000-0.9999999 seconds)

Note: Use between one and seven ‘f’ characters.
UTC offsetK (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 stringExample
“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
“MMMM d (dddd)”May 4 (Thursday)

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. 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):

“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)

Leave a Comment