The syntax of a format item is as follows:
{index[,length][:formatString]}
index The
zero-based position in the parameter list of the object to be
formatted. If the object specified by index is null
reference (Nothing in Visual Basic), the format item is replaced by
String..::.Empty . Because this overload has only a single object in
its parameter list, the value of index must always be 0. If there is no
parameter in the index position, a FormatException is thrown.,length The
minimum number of characters in the string representation of the
parameter. If positive, the parameter is right-aligned; if negative, it
is left-aligned.:formatString A standard or custom format string that is supported by the parameter.
Formatting Numbers (Standard Format Specifiers)
// Standard Numeric Format Specifiers // ------------------------------------------------------------- {0:C}, -123 Currency: . . . . . . . . ($123.00) {0:D}, -123 Decimal:. . . . . . . . . -123 {1:E},-123.45f Scientific: . . . . . . . -1.234500E+002 {1:F},-123.45f Fixed point:. . . . . . . -123.45 {0:G}, -123 General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') {0:N}, -123 Number: . . . . . . . . . -123.00 {1:P},-123.45f Percent:. . . . . . . . . -12,345.00 % {1:R},-123.45f Round-trip: . . . . . . . -123.45 {0:X}, -123 Hexadecimal:. . . . . . . FFFFFF85
Formatting Enumerations (Standard Format Specifiers)
// Standard Enumeration Format Specifiers // ------------------------------------------------------------- enum Color {Yellow = 1, Blue, Green}; {0:G},Color.Green General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') {0:F},Color.Green Flags:. . . . . . . . . . Green (flags or integer) {0:D},Color.Green Decimal number: . . . . . 3 {0:X},Color.Green Hexadecimal:. . . . . . . 00000003
Formatting DateTime (Standard Format Specifiers)
// Standard DateTime Format Specifiers // ------------------------------------------------------------- // Usage of the Format Specifiers // String Specifier = ""; // String.Format(Specifier, DateTime.Now); // Short date {0:d} // 8/18/2009 // Long date {0:D} // Tuesday, August 18, 2009 // Short time {0:t} // 10:52 PM // Long time {0:T} // 10:52:22 PM // Full date/short time {0:f} // Tuesday, August 18, 2009 10:52 PM // Full date/long time {0:F} // Tuesday, August 18, 2009 10:52:22 PM // General date/short time {0:g} // 8/18/2009 10:52 PM // General date/long time {0:G} // 8/18/2009 10:52:22 PM // Month {0:M} // August 18 // RFC1123 {0:R} // Tue, 18 Aug 2009 22:52:22 GMT // Sortable {0:s} // 2009-08-18T22:52:22 // Universal sortable {0:u} // 2009-08-18 22:52:22Z (invariant) // Universal full date/time {0:U} // Wednesday, August 19, 2009 3:52:22 AM // Year {0:Y} // August, 2009 // Formatted DateTime {0:MM/dd/yyyy} // 08/18/2009 {0:yyyy-MM-dd} // 2009-08-18
Formatting DateTime (DateTime.ToString() Method)
// Standard DateTime (DateTime.ToString() Method) // Format Specifiers // ------------------------------------------------------------- DateTime dt = DateTime.Now; dt.ToString(""); "d" // 8/18/2009 Short Date "%d" // 18 Day number "M?d" // 8?18 Month and day number "dd" // 18 Day number, two digits "ddd" // Tue Abbreviated day name "dddd" // Tuesday Full day name "f" // Tuesday, August 18, 2009 10:34 PM Full (long date, short time) "%f" // 6 Fractions of second, one digit "s^f" // 32^6 Seconds and fractions of second, one digit "ff" // 60 Fractions of second, two digits "fff" // 609 Fractions of second, three digits "ffff" // 6093 Fractions of second, four digits "fffff" // 60937 Fractions of second, five digits "ffffff" // 609375 Fractions of second, six digits "fffffff" // 6093750 Fractions of second, seven digits "g" // 8/18/2009 10:34 PM General "%g" // A.D. Era (eg. A.D.) "y-g" // 9-A.D. Year and era (eg. 5-A.D.) "gg" // A.D. Era (eg. A.D.) "%h" // 10 Hour (1-12) "h-m" // 10-34 Hour and minute "hh" // 10 Hour (01-12) "HH" // 22 Hour (00-23) "m" // August 18 Month name and date "%m" // 34 Minute (0-59) "hh_m" // 10_34 Hour and minute (0-59) "mm" // 34 Minute (00-59) "M" // August 18 Month name and date "%M" // 8 Month number (1-12) "M+d" // 8+18 Month number and day number "MM" // 08 Month number (01-12) "MMM" // Aug Month abbreviation "MMMM" // August Month name "s" // 2009-08-18T22:34:32 Standard sortable date/time "%s" // 32 Seconds (0-59) "s^ff" // 32^60 Seconds (0-59) and fraction of seconds "ss" // 32 Seconds (00-59) "t" // 10:34 PM Long time "%t" // P First letter of AM/PM designator "hh+t" // 10+P Hour and first letter of AM/PM designator "tt" // PM AM/PM designator "y" // August, 2009 ShortDate "m-y" // 34-9 Month and year "yy" // 09 Year (00-99) "yyyy" // 2009 Year (0000-9999) "%z" // -5 Whole hour time zone (-12 to +13) "Zone:z" // Zone:-5 Zone - and whole hour time zone (-12 to +13) "zz" // -05 Whole hour time zone (-12 to +13) with two digits "zzz" // -05:00 Time zone in hours and minutes
Formatting DateTime (Standard DateTime Methods)
// Standard DateTime (Standard DateTime Methods) Format Specifiers // ------------------------------------------------------------- DateTime dt = DateTime.Now; dt.Date // 8/18/2009 12:00:00 AM dt.Day // 18 dt.DayOfWeek // Tuesday dt.Hour // 22 dt.IsDaylightSavingTime() // True dt.Kind // Local dt.Millisecond // 609 dt.Minute // 34 dt.Month // 8 dt.Second // 32 dt.Ticks // 633862316726093750 dt.TimeOfDay // 22:34:32.6093750 dt.ToFileTime() // 128951264726093750 dt.ToFileTimeUtc() // 128951264726093750 dt.ToLocalTime() // 8/18/2009 10:34:32 PM dt.ToLongDateString() // Tuesday, August 18, 2009 dt.ToLongTimeString() // 10:34:32 PM dt.ToOADate() // 40043.9406551968 dt.ToShortDateString() // 8/18/2009 dt.ToShortTimeString() // 10:34 PM dt.ToUniversalTime() // 8/19/2009 3:34:32 AM dt.Year // 2009
No comments:
Post a Comment