A program uses date and time format specifiers to determine how dates and times are represented as strings. C# provides two kinds of specifiers that you can use to determine a date and time value’s format: standard format specifiers and custom format specifiers.
Standard format specifiers are locale-aware, so the result depends on the computer’s regional settings. For that reason, you should always use the standard specifiers whenever possible.
The following sections describe the available standard and custom date and time format specifiers.
A standard format specifier is a single character that you use alone to indicate a standardized format. For example, the format string d
indicates a short date format (as in 8/20/2012).
The following table lists standard format specifiers that you can use to format date and time strings. The examples shown in this table are for a typical computer in the United States.
Specifier | Meaning | Example |
d | Short date. | 8/20/2015 |
D | Long date. | Thursday, August 20, 2015 |
t | Short time. | 2:37 PM |
T | Long time. | 2:37:18 PM |
f | Full date/time with short time. | Thursday, August 20, 2015 2:37 PM |
F | Full date/time with long time. | Thursday, August 20, 2015 2:37:18 PM |
g | General date/time with short time. | 8/20/2015 2:37 PM |
G | General date/time with long time. | 8/20/2015 2:37:18 PM |
m or M | Month and date. | August 20 |
r or R | RFC1123 pattern. Formatting does not convert the time to Greenwich Mean Time (GMT), so you should convert local times to GMT before formatting. | Thu, 20 Aug 2015 14:37:18 GMT |
S | Sortable ISO 8601 date/time. | 2015-08-20T14:37:18 |
u | Universal sortable date/time. Formatting does not convert the time to universal time, so you should convert local times to universal time before formatting. | 2015-08-20 14:37:18Z |
U | Universal full date/time. This is the full universal time, not the local time. | Thursday, August 20, 2015 9:37:18 PM |
y or Y | Year and month. | August 2015 |
Custom format specifiers describe pieces of a date or time that you can use to build your own customized formats. For example, the specifier ddd
indicates the abbreviated day of the week, as in Wed.
In general, you should use custom date and time formats only to build values used inside the code. The user should never see them.
The following table lists characters that you can use to build custom formats for date and time strings.
Specifier | Meaning | Example |
d | Date of the month (1–31). | 3 |
dd | Date of the month with two digits (01–31). | 03 |
ddd | Abbreviated day of the week. | Wed |
dddd | Full day of the week. | Wednesday |
f | Fractions of seconds, one digit. Add additional f s for up to seven digits (fffffff ). | 8 |
F | Similar to f except nothing is displayed if the fraction is 0. | 8 |
g or gg | Era. | A.D. |
h | Hour, 12-hour clock with one digit (1–12). | 1 |
hh | Hour, 12-hour clock with two digits (01–12). | 01 |
H | Hour, 24-hour clock with one digit (1–24). | 13 |
HH | Hour, 24-hour clock with two digits (01–24). | 07 |
K | Time zone information. | –07:00 |
m | Minutes with one digit (0–59). | 9 |
mm | Minutes with two digits (00–59). | 09 |
M | Month number with one digit (1–12). | 2 |
MM | Month number with two digits (01–12). | 02 |
MMM | Month abbreviation. | Feb |
MMMM | Full month name. | February |
s | Seconds with one digit (0–59). | 3 |
ss | Seconds with two digits (00–59). | 03 |
t | AM/PM designator with one character. | A |
tt | AM/PM designator with two characters. | AM |
y | Year with up to two digits (0–99). | 7 |
yy | Year with two digits (00–99). | 07 |
yyyy | Year with four digits. | 2015 |
yyyyy | Year with five digits. | 02015 |
z | Time zone offset (hours from GMT in the range –12 to +13). | –7 |
zz | Time zone offset with two digits. | –07 |
zzz | Time zone offset with two digits of hours and minutes. | –07:00 |
: | Time separator. | |
/ | Date separator. | |
"..." | Quoted string. Displays the enclosed characters without trying to interpret them. | |
'...' | Quoted string. Displays the enclosed characters without trying to interpret them. | |
% | Displays the following character as a custom specifier. (See the following discussion.) | |
| Displays the next character without trying to interpret it. |
Some of the custom specifier characters in this table are the same as characters used by standard specifiers. For example, if you use the character d
alone, C# interprets it as the standard specifier for a short date. If you use the character d
in a custom specifier, C# interprets it as the date of the month.
If you want to use a custom specifier alone, precede it with the %
character. The following shows two queries and their results executed in the Immediate window.
DateTime.Now.ToString("d")
"4/1/2015"
DateTime.Now.ToString("%d")
"1"
Custom specifiers are somewhat sensitive to the computer’s regional settings. For example, they at least know the local names and abbreviations of the months and days of the week.
The standard specifiers have even more information about the local culture, however. For example, the date specifiers know whether the local culture places months before or after days. The d
specifier gives the result 8/20/2015 for the en-US culture (English, United States), and it returns 20/08/2015 for the culture en-NZ (English, New Zealand).
To avoid cultural problems on different computers, you should use the standard specifiers whenever possible rather than build your own custom format specifiers. For example, use d
instead of M/d/yyyy
.
3.128.206.48