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.

Open table as spreadsheet

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.

4

yy

Year with two digits.

04

yyyy

Year with four digits.

2004

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

To simplify cultural differences, you should use the standard specifiers whenever they will satisfy your needs rather than building your own custom format specifiers. For example, use d instead of M/d/yyyy.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net