Appendix P
Date and Time Format Specifiers

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.

Standard 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.

SpecifierMeaningExample
dShort date.8/20/2015
DLong date.Thursday, August 20, 2015
tShort time.2:37 PM
TLong time.2:37:18 PM
fFull date/time with short time.Thursday, August 20, 2015 2:37 PM
FFull date/time with long time.Thursday, August 20, 2015 2:37:18 PM
gGeneral date/time with short time.8/20/2015 2:37 PM
GGeneral date/time with long time.8/20/2015 2:37:18 PM
m or MMonth and date.August 20
r or RRFC1123 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
SSortable ISO 8601 date/time.2015-08-20T14:37:18
uUniversal 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
UUniversal full date/time. This is the full universal time, not the local time.Thursday, August 20, 2015 9:37:18 PM
y or YYear and month.August 2015

Custom Format Specifiers

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.

SpecifierMeaningExample
dDate of the month (1–31).3
ddDate of the month with two digits (01–31).03
dddAbbreviated day of the week.Wed
ddddFull day of the week.Wednesday
fFractions of seconds, one digit. Add additional fs for up to seven digits (fffffff).8
FSimilar to f except nothing is displayed if the fraction is 0.8
g or ggEra.A.D.
hHour, 12-hour clock with one digit (1–12).1
hhHour, 12-hour clock with two digits (01–12).01
HHour, 24-hour clock with one digit (1–24).13
HHHour, 24-hour clock with two digits (01–24).07
KTime zone information.–07:00
mMinutes with one digit (0–59).9
mmMinutes with two digits (00–59).09
MMonth number with one digit (1–12).2
MMMonth number with two digits (01–12).02
MMMMonth abbreviation.Feb
MMMMFull month name.February
sSeconds with one digit (0–59).3
ssSeconds with two digits (00–59).03
tAM/PM designator with one character.A
ttAM/PM designator with two characters.AM
yYear with up to two digits (0–99).7
yyYear with two digits (00–99).07
yyyyYear with four digits.2015
yyyyyYear with five digits.02015
zTime zone offset (hours from GMT in the range –12 to +13).–7
zzTime zone offset with two digits.–07
zzzTime 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.206.48