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. For example, the Date object’s ToString method returns a string representing a date and time. An optional parameter to this method tells the object whether to format itself as in 8/20/2012, 08.20.12 A.D., or Monday, August 20, 2012 2:37:18 pm.

Visual Basic 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

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 results depend on the regional settings on the computer. The examples shown in this table are for a typical computer in the United States.

SPECIFIER MEANING EXAMPLE
d Short date. 8/20/2012
D Long date. Monday, August 20, 2012
t Short time. 2:37 PM
T Long time. 2:37:18 PM
f Full date/time with short time. Monday, August 20, 2012 2:37 PM
F Full date/time with long time. Monday, August 20, 2012 2:37:18 PM
g General date/time with short time. 8/20/2012 2:37 PM
G General date/time with long time. 8/20/2012 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. Mon, 20 Aug 2012 14:37:18 GMT
S Sortable ISO 8601 date/time. 2012-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. 2012-08-20 14:37:18Z
U Universal full date/time. This is the full universal time, not the local time. Monday, August 20, 2012 9:37:18 PM
y or Y Year and month. August 2012

You can learn more about RFC1123 at http://www.faqs.org/rfcs/rfc1123.html. You can learn more about ISO 8601 at http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/date_and_time_format.htm.

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.

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. 3
dd Date of the month with two digits. 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
g Era. A.D.
h Hour, 12-hour clock with one digit, if possible. 1
hh Hour, 12-hour clock with two digits. 01
H Hour, 24-hour clock with one digit, if possible. 13
HH Hour, 24-hour clock with two digits. 07
m Minutes with one digit, if possible. 9
mm Minutes with two digits. 09
M Month number (1–12) with one digit, if possible. 2
MM Month number (1–12) with two digits. 02
MMM Month abbreviation. Feb
MMMM Full month name. February
s Seconds with one digit, if possible. 3
ss Seconds with two digits. 03
t AM/PM designator with one character. A
tt AM/PM designator with two characters. AM
y Year with up to two digits, not zero-padded. 12
yy Year with two digits. 12
yyyy Year with four digits. 2012
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, Visual Basic interprets it as the standard specifier for a short date. If you use the character d in a custom specifier, Visual Basic 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:

?Now.ToString("d")
"8/20/2012"
?Now.ToString("%d")
"20"

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/2012 for the en-US culture (English, United States), and it returns 20/08/2012 for the culture en-NZ (English, New Zealand).

To avoid cultural problems on different computers, you should use the standard specifiers whenever they will satisfy your needs 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.139.70.248