Using Picture Formats for Custom Formatting
The standard format specifiers are useful in most cases whilst still maintaining the correct style of date and time for the user's location. On some occasions however, it is necessary to have complete control over the positioning and styling of each element of date and time information.For this, picture formats must be used. These allow an exact format to be created using any combination of elements from a DateTime.
DateTime theDate = DateTime.Parse("3 Jan 2007 21:25:30"); string result; result = theDate.ToString("d-MM-yy");//result ="3-01-07" result = theDate.ToString("HH:mm");//result ="21:25" result = theDate.ToString("h:mm tt");//result ="9:25 PM"
Available Picture Formatting Codes
The above example shows several formats and the results. The full list of available formatting codes is as follows:Specifier | Description | Examples |
---|---|---|
y | One-digit year. If the year cannot be specified in one digit then two digits are used automatically. | "7" "95" |
yy | Two-digit year with leading zeroes if required. | "07" |
yyyy | Full four-digit year. | "2007" |
g or gg | Indicator of Anno Domini (AD). | "A.D." |
M | One-digit month number. If the month cannot be specified in one digit then two digits are used automatically. | "1" "12" |
MM | Two-digit month number with leading zeroes if required. | "01" |
MMM | Three letter month abbreviation. | "Jan" |
MMMM | Month name. | "January" |
d | One-digit day number. If the day cannot be specified in one digit then two digits are used automatically. | "3" "31" |
dd | Two-digit day number with leading zeroes if required. | "03" |
ddd | Three letter day name abbreviation. | "Wed" |
dddd | Day name. | "Wednesday" |
h | One-digit hour using the twelve hour clock. If the hour cannot be specified in one digit then two digits are used automatically. | "9" "12" |
hh | Two-digit hour using the twelve hour clock with leading zeroes if required. | "09" |
H | One-digit hour using the twenty four hour clock. If the hour cannot be specified in one digit then two digits are used automatically. | "1" "21" |
HH | Two-digit hour using the twenty four hour clock with leading zeroes if required. | "09" |
t | Single letter indicator of AM or PM, generally for use with twelve hour clock values. | "A" "P" |
tt | Two letter indicator of AM or PM, generally for use with twelve hour clock values. | "AM" "PM" |
m | One-digit minute. If the minute cannot be specified in one digit then two digits are used automatically. | "1" "15" |
mm | Two-digit minute with leading zeroes if required. | "01" |
s | One-digit second. If the second cannot be specified in one digit then two digits are used automatically. | "1" "59" |
ss | Two-digit second with leading zeroes if required. | "01" |
f | Fraction of a second. Up to seven f's can be included to determine the number of decimal places to display. | "0" "0000000" |
z | One-digit time zone offset indicating the difference in hours between local time and UTC time. If the offset cannot be specified in one digit then two digits are used automatically. | "+6" "-1" |
zz | Two-digit time zone offset indicating the difference in hours between local time and UTC time with leading zeroes if required. | "+06" |
zzz | Full time zone offset indicating the difference in hours and minutes between local time and UTC time with leading zeroes if required. | "+06:00" |
Some of the formatting codes use the same letter as a format specifier. When used within a format string that is greater than one character in length this does not present a problem.
If you need to use a single character picture format string, the letter must be preceded by a percentage sign (%) to indicate that the standard format specifier is not to be used.
DateTime theDate = DateTime.Parse("3 Jan 2007 21:25:30"); string result; result = theDate.ToString("d");// result = "03/01/2007" result = theDate.ToString("%d");// result = "3"
No comments:
Post a Comment