DateTime.ToString Method (String)
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| The length of format is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo. -or- format does not contain a valid custom format pattern. |
The format parameter should contain either a format specifier character or a custom format pattern. For more information, see the summary page for System.Globalization.DateTimeFormatInfo.
If format is a null reference (Nothing in Visual Basic) or an empty string, the general format specifier, 'G', is used.
The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:
-
For more information about date and time format specifiers, see Standard DateTime Format Strings and Custom DateTime Format Strings.
-
For more information about formatting, see Formatting Types and Formatting Overview.
This method uses formatting information derived from the current culture. For more information, see CurrentCulture.
The following code example formats a DateTime object using the thread current culture, a specified culture, and all the standard date and time format specifiers.
// This code example demonstrates the ToString(String) and // ToString(String, IFormatProvider) methods for the DateTime // type in conjunction with the standard date and time // format specifiers. using namespace System; using namespace System::Globalization; using namespace System::Threading; int main() { String^ msgShortDate = "(d) Short date: . . . . . . . "; String^ msgLongDate = "(D) Long date:. . . . . . . . "; String^ msgShortTime = "(t) Short time: . . . . . . . "; String^ msgLongTime = "(T) Long time:. . . . . . . . "; String^ msgFullDateShortTime = "(f) Full date/short time: . . "; String^ msgFullDateLongTime = "(F) Full date/long time:. . . "; String^ msgGeneralDateShortTime = "(g) General date/short time:. "; String^ msgGeneralDateLongTime = "(G) General date/long time (default):\n" + " . . . . . . . . . . . . . "; String^ msgMonth = "(M) Month:. . . . . . . . . . "; String^ msgRFC1123 = "(R) RFC1123:. . . . . . . . . "; String^ msgSortable = "(s) Sortable: . . . . . . . . "; String^ msgUniSortInvariant = "(u) Universal sortable (invariant):\n" + " . . . . . . . . . . . . . "; String^ msgUniSort = "(U) Universal sortable: . . . "; String^ msgYear = "(Y) Year: . . . . . . . . . . "; String^ msgRoundtripLocal = "(o) Roundtrip (local):. . . . "; String^ msgRoundtripUTC = "(o) Roundtrip (UTC):. . . . . "; String^ msgRoundtripUnspecified = "(o) Roundtrip (Unspecified):. "; String^ msg1 = "Use ToString(String) and the current thread culture.\n"; String^ msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n"; String^ msgCulture = "Culture:"; String^ msgThisDate = "This date and time: {0}\n"; DateTime^ thisDate = DateTime::Now; DateTime^ utcDate = thisDate->ToUniversalTime(); DateTime^ unspecifiedDate = gcnew DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind::Unspecified); CultureInfo^ ci; // Format the current date and time in various ways. Console::Clear(); Console::WriteLine("Standard DateTime Format Specifiers:\n"); Console::WriteLine(msgThisDate, thisDate); Console::WriteLine(msg1); // Display the thread current culture, which is used to format the values. ci = Thread::CurrentThread->CurrentCulture; Console::WriteLine("{0,-30}{1}\n", msgCulture, ci->DisplayName); Console::WriteLine(msgShortDate + thisDate->ToString("d")); Console::WriteLine(msgLongDate + thisDate->ToString("D")); Console::WriteLine(msgShortTime + thisDate->ToString("t")); Console::WriteLine(msgLongTime + thisDate->ToString("T")); Console::WriteLine(msgFullDateShortTime + thisDate->ToString("f")); Console::WriteLine(msgFullDateLongTime + thisDate->ToString("F")); Console::WriteLine(msgGeneralDateShortTime + thisDate->ToString("g")); Console::WriteLine(msgGeneralDateLongTime + thisDate->ToString("G")); Console::WriteLine(msgMonth + thisDate->ToString("M")); Console::WriteLine(msgRFC1123 + utcDate->ToString("R")); Console::WriteLine(msgSortable + thisDate->ToString("s")); Console::WriteLine(msgUniSortInvariant + utcDate->ToString("u")); Console::WriteLine(msgUniSort + thisDate->ToString("U")); Console::WriteLine(msgYear + thisDate->ToString("Y")); Console::WriteLine(msgRoundtripLocal + thisDate->ToString("o")); Console::WriteLine(msgRoundtripUTC + utcDate->ToString("o")); Console::WriteLine(msgRoundtripUnspecified + unspecifiedDate->ToString("o")); Console::WriteLine(); // Display the same values using a CultureInfo object. The CultureInfo class // implements IFormatProvider. Console::WriteLine(msg2); // Display the culture used to format the values. ci = gcnew CultureInfo("de-DE"); Console::WriteLine("{0,-30}{1}\n", msgCulture, ci->DisplayName); Console::WriteLine(msgShortDate + thisDate->ToString("d", ci)); Console::WriteLine(msgLongDate + thisDate->ToString("D", ci)); Console::WriteLine(msgShortTime + thisDate->ToString("t", ci)); Console::WriteLine(msgLongTime + thisDate->ToString("T", ci)); Console::WriteLine(msgFullDateShortTime + thisDate->ToString("f", ci)); Console::WriteLine(msgFullDateLongTime + thisDate->ToString("F", ci)); Console::WriteLine(msgGeneralDateShortTime + thisDate->ToString("g", ci)); Console::WriteLine(msgGeneralDateLongTime + thisDate->ToString("G", ci)); Console::WriteLine(msgMonth + thisDate->ToString("M", ci)); Console::WriteLine(msgRFC1123 + utcDate->ToString("R", ci)); Console::WriteLine(msgSortable + thisDate->ToString("s", ci)); Console::WriteLine(msgUniSortInvariant + utcDate->ToString("u", ci)); Console::WriteLine(msgUniSort + thisDate->ToString("U", ci)); Console::WriteLine(msgYear + thisDate->ToString("Y", ci)); Console::WriteLine(msgRoundtripLocal + thisDate->ToString("o", ci)); Console::WriteLine(msgRoundtripUTC + utcDate->ToString("o", ci)); Console::WriteLine(msgRoundtripUnspecified + unspecifiedDate->ToString("o", ci)); Console::WriteLine(); } /* This code example produces the following results: Standard DateTime Format Specifiers: This date and time: 4/17/2006 2:38:09 PM Use ToString(String) and the current thread culture. Culture: English (United States) (d) Short date: . . . . . . . 4/17/2006 (D) Long date:. . . . . . . . Monday, April 17, 2006 (t) Short time: . . . . . . . 2:38 PM (T) Long time:. . . . . . . . 2:38:09 PM (f) Full date/short time: . . Monday, April 17, 2006 2:38 PM (F) Full date/long time:. . . Monday, April 17, 2006 2:38:09 PM (g) General date/short time:. 4/17/2006 2:38 PM (G) General date/long time (default): . . . . . . . . . . . . . 4/17/2006 2:38:09 PM (M) Month:. . . . . . . . . . April 17 (R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:38:09 GMT (s) Sortable: . . . . . . . . 2006-04-17T14:38:09 (u) Universal sortable (invariant): . . . . . . . . . . . . . 2006-04-17 21:38:09Z (U) Universal sortable: . . . Monday, April 17, 2006 9:38:09 PM (Y) Year: . . . . . . . . . . April, 2006 (o) Roundtrip (local):. . . . 2006-04-17T14:38:09.9417500-07:00 (o) Roundtrip (UTC):. . . . . 2006-04-17T21:38:09.9417500Z (o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000 Use ToString(String, IFormatProvider) and a specified culture. Culture: German (Germany) (d) Short date: . . . . . . . 17.04.2006 (D) Long date:. . . . . . . . Montag, 17. April 2006 (t) Short time: . . . . . . . 14:38 (T) Long time:. . . . . . . . 14:38:09 (f) Full date/short time: . . Montag, 17. April 2006 14:38 (F) Full date/long time:. . . Montag, 17. April 2006 14:38:09 (g) General date/short time:. 17.04.2006 14:38 (G) General date/long time (default): . . . . . . . . . . . . . 17.04.2006 14:38:09 (M) Month:. . . . . . . . . . 17 April (R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:38:09 GMT (s) Sortable: . . . . . . . . 2006-04-17T14:38:09 (u) Universal sortable (invariant): . . . . . . . . . . . . . 2006-04-17 21:38:09Z (U) Universal sortable: . . . Montag, 17. April 2006 21:38:09 (Y) Year: . . . . . . . . . . April 2006 (o) Roundtrip (local):. . . . 2006-04-17T14:38:09.9417500-07:00 (o) Roundtrip (UTC):. . . . . 2006-04-17T21:38:09.9417500Z (o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000 */
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.