DateTime.ToString (String, IFormatProvider)
parameter format verbose usage :
format character |
Association properties/Description |
d |
Shortdat Epattern |
D |
longdatepattern |
f |
full date and time (long date and short time |
F |
fulldatetimepattern (long date and long time) |
G |
General (short date and short term) Room) |
G |
General (short date and long time) |
m, m |
Monthdaypattern |
R, R |
rfc1123pattern |
s |
use local time Sortabledatetimepatter N (based on ISO 8601) |
t |
shorttimepattern |
t |
LONGTIMEP Attern |
u |
universalsortabledatetimepattern format for displaying Universal Time |
u |
Full date and time with Universal Time (long date and long time) |
y, y |
yearmonthpattern |
The following table lists the patterns that can be combined to construct a custom pattern. These patterns are case-sensitive, for example, "MM" is recognized, but "MM" is not recognized. If the custom pattern contains white space characters or characters enclosed in single quotation marks, the output string page will also contain these characters. Characters that are not defined as part of the format pattern or that are not defined as format characters are copied verbatim.
Format pattern |
Description |
D |
One day of the month. One-digit date has no leading zeros. |
Dd |
One day of the month. A one-digit date has a leading zero. |
Ddd |
Abbreviated name of the day of the week, defined in Abbreviateddaynames. |
dddd |
The full name of the day of the week, as defined in DayNames. |
M |
Month number. One-digit month has no leading zeros. |
Mm |
Month number. One-digit month has a leading zero. |
MMM |
Abbreviated name of the month, defined in AbbreviatedMonthNames. |
MMMM |
The full name of the month, as defined in MonthNames. |
Y |
The year that does not contain eras. If the year that does not contain an era is less than 10, the year is displayed without leading zeros. |
Yy |
The year that does not contain eras. If the year that does not contain an era is less than 10, the year with leading zeros is displayed. |
yyyy |
The year that includes the four-digit era. |
Gg |
Period or epoch. If the date to be formatted does not have an associated period or era string, the pattern is ignored. |
H |
12-hour hour. One-digit hours do not have leading zeros. |
hh |
12-hour hour. One-digit hours have leading zeros. |
H |
24-hour hour. One-digit hours do not have leading zeros. |
HH |
24-hour hour. One-digit hours have leading zeros. |
M |
Minutes, one-digit minutes without leading zeros. |
Mm |
Minutes, one-digit minutes have a leading zero. |
S |
Seconds, the number of seconds of one digit does not have a leading zero. |
Ss |
Seconds, the number of seconds of one digit has a leading zero. |
F |
The fractional precision of the second is one bit. The remaining digits are truncated. |
Ff |
The fractional seconds precision is two bits. The remaining digits are truncated. |
Fff |
The fractional seconds precision is three bits. The remaining digits are truncated. |
Ffff |
The fractional seconds precision is four bits. The remaining digits are truncated. |
Fffff |
The fractional seconds precision is five bits. The remaining digits are truncated. |
Ffffff |
The fractional seconds precision is six bits. The remaining digits are truncated. |
Fffffff |
The fractional seconds precision is seven bits. The remaining digits are truncated. |
T |
The am/pm defined in AMDesignator or PMDesignator indicates the first character of the item, if one exists. |
Tt |
The am/pm that is defined in AMDesignator or pmdesignator, if present. |
Z |
The time zone offset ("+" or "-" followed by hours only). One-digit hours do not have leading zeros. For example, Pacific Standard Time is "-8". |
Zz |
The time zone offset ("+" or "-" followed by hours only). One-digit hours have leading zeros. For example, Pacific Standard Time is "-08". |
zzz |
The full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes of a single digit has a leading zero. For example, Pacific Standard Time is " -08:00". |
: |
The default time delimiter that is defined in TimeSeparator. |
/ |
The default date delimiter that is defined in DateSeparator. |
%c |
where c is the format pattern (if used alone). You can omit the "%" character if the format pattern is merged with literal characters or other format patterns. |
C |
where c is any character. Displays the characters as they are originally defined. To display a backslash character, use "\". |
Only the format patterns listed in the second table above can be used to create custom patterns, and the standard format characters listed in the first table cannot be used to create custom patterns. The custom pattern is at least two characters long;
DateTime.ToString ("D") returns a DateTime value; "D" is a standard short date pattern.
DateTime.ToString ("%d") returns the day of the month; "%d" is a custom pattern.
DateTime.ToString ("D") returns the day of the month followed by a blank character, and "D" is a custom pattern.
C # time formatting (Datetime) usage