The Comprehensive Guide to Custom DateTime String Formats in C#
When working with C#, we can convert a DateTime object to a string. If we don't specify any formatting options, the default DateTime string format will be outputted. However, C# gives us the power to completely customise how the DateTime object should be outputted using custom format specifiers.
How do I format DateTime objects?
There are a few ways that you can format a DateTime object when converting it to a string. We'll first go through some of these methods before going through how to use custom format strings to format DateTime objects.
DateTime.ToString()
This is probably the most common way of formatting a DateTime as a string in C#. It allows you to optionally pass the format to use as a string parameter. For example (click Run to run the example):
You can read more information here.
string.Format()
You are also able to specify how a string should be formatted using string.Format() by passing in the DateTime object as one of the objects to be inserted. For example (click Run to run the example):
You can read more here.
String Interpolation ($)
This is by far the neatest way to format a DateTime string in my opinion. It looks neat and is compact. Here's an example (click Run to run the example):
You can read more about string interpolation here.
Custom DateTime Format Strings
We are able to format a DateTime using a custom format string. A format string is simply a string in C# where sequences of characters (called format specifiers) in the string dictate what portions of the DateTime should be outputted and how they should be formatted. These format specifiers can be combined to output multiple portions of a DateTime object (as well as other objects in .NET) in a single string. This gives you granular control over how DateTime objects should be outputted.
Below is a table of all the different string sequences that can be used in custom format strings for DateTime objects (taken from here). There is a REPL below that you can use to test these format specifiers.
Format Specifier | DateTime Segment | Description |
---|---|---|
"g" or "gg" |
Era | Outputs the era ("B.C." or "A.D." ). Note: Only use "g" for the era when it is used with other format specifiers in the same string (adding "''" will work) otherwise it is interpreted as a standard date and time format specifier. |
"y" |
Year | The year from 0 to 99 (excluding the century) without any leading zeros Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples:
|
"yy" |
Year | The year from 00 to 99 (excluding the century) with a leading zero where necessary so that the year is always two characters. Examples:
|
"yyy" |
Year | The year, with a minimum of three digits. Examples:
|
"yyyy" |
Year | The year as a four-digit number. Examples:
|
"yyyyy" |
Year | The year as a five-digit number. Examples:
|
"M" |
Month | The month of the year (from 1 to 12) without any leading zeros. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples:
|
"MM" |
Month | The month of the year (from 01 to 12) with a leading zero. Examples:
|
"MMM" |
Month | The abbreviated name of the month. This differs based on language and region. Examples:
|
"MMMM" |
Month | The full name of the month. This differs based on language and region. Examples:
|
"d" |
Day | The day of the month (from 1 to 31) without any leading zeros. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples:
|
"dd" |
Day | The day of the month (from 1 to 31) with a leading zero where necessary so that the date is always two characters. Examples:
|
"ddd" |
Day | The abbreviated name of the day of the week. Examples: Mon, Tues |
"dddd" |
Day | The full name of the day of the week. Examples: Monday, Tuesday |
"t" |
AM/PM | The first character of the AM/PM designator. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples:
|
"tt" |
AM/PM | The AM/PM designator. Examples:
|
"h" |
Hour | The hour, using a 12-hour clock (from 1 to 12) without a leading zero. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples: 11 for 11:00:00AM, 1 for 1:00:00PM |
"hh" |
Hour | The hour, using a 12-hour clock (from 01 to 12) with a leading zero. Examples: 11 for 11:00:00AM, 01 for 1:00:00PM |
"H" |
Hour | The hour, using a 24-hour clock (from 0 to 23) without a leading zero. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples: 9 for 9:00:00AM, 11 for 11:00:00AM, 13 for 1:00:00PM |
"HH" |
Hour | The hour, using a 24-hour clock (from 00 to 23) with a leading zero. Examples: 09 for 9:00:00AM, 11 for 11:00:00AM, 13 for 1:00:00PM |
"m" |
Minute | The minute, from 0 to 59, without a leading zero. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples: 8 for 9:08:00AM, 20 for 1:20:00PM |
"mm" |
Minute | The minute, from 00 to 59, with a leading zero. Examples: 08 for 9:08:00AM, 20 for 1:20:00PM |
"s" |
Second | The second, from 0 to 59, without a leading zero. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples: 3 for 9:08:03AM, 45 for 1:20:45PM |
"ss" |
Second | The second, from 00 to 59, with a leading zero. Examples: 03 for 9:08:03AM, 45 for 1:20:45PM |
"f" |
Fraction of Second | The tenths of a second, from 0 to 9 Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples:
|
"ff" |
Fraction of Second | The hundredths of a second, from 00 to 99 Examples:
|
"fff" |
Fraction of Second | The milliseconds, from 000 to 999 Examples:
|
"ffff" |
Fraction of Second | The ten thousandths of a second, from 0000 to 9999 Examples:
|
"fffff" |
Fraction of Second | The hundred thousandths of a second, from 00000 to 99999 Examples:
|
"ffffff" |
Fraction of Second | The millionths of a second, from 000000 to 999999 Examples:
|
"fffffff" |
Fraction of Second | The ten millionths of a second, from 0000000 to 9999999 Examples:
|
"F" |
Fraction of Second | The tenths of a second, from 0 to 9. Only outputted if the value is non-zero otherwise an empty is outputted. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples:
|
"FF" |
Fraction of Second | The hundredths of a second, from 00 to 99. Only outputted if the value is non-zero otherwise an empty is outputted. Examples:
|
"FFF" |
Fraction of Second | The milliseconds, from 000 to 999. Only outputted if the value is non-zero otherwise an empty is outputted. Examples:
|
"FFFF" |
Fraction of Second | The ten thousandths of a second, from 0000 to 9999. Only outputted if the value is non-zero otherwise an empty is outputted. Examples:
|
"FFFFF" |
Fraction of Second | The hundred thousandths of a second, from 00000 to 99999. Only outputted if the value is non-zero otherwise an empty is outputted. Examples:
|
"FFFFFF" |
Fraction of Second | The millionths of a second, from 000000 to 999999. Only outputted if the value is non-zero otherwise an empty is outputted. Examples:
|
"FFFFFFF" |
Fraction of Second | The ten millionths of a second, from 0000000 to 9999999. Only outputted if the value is non-zero otherwise an empty is outputted. Examples:
|
"K" |
Time zone | This depends on the Kind of DateTime object. Kinds:
"''" will work). |
"z" |
Time zone | Hours offset from UTC, with no leading zeros Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples:
|
"zz" |
Time zone | Hours offset from UTC, with a leading zero for single-digit values Examples:
|
"zzz" |
Time zone | Hours and minutes offset from UTC Examples:
|
"/" |
Misc | The date separator. This is determined based on the region and language. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples:
|
":" |
Misc | The time separator. This is determined based on the region and language. Note: This will only work when used with other format specifiers in the same string (adding "''" will work).Examples:
|
"'<STRING>'" |
Misc | Literal string delimiter. This can be used to escape a string. |
"\<CHAR>" |
Misc | Escape character. This can be used to escape a single character in a format specifier |
Here is a REPL that demonstrating all the above Format Specifiers. Feel free to experiment!
I hope you found this article informative. Please let me know if you have any suggestions, queries or criticisms.