Standard DateTime format string

Source: Internet
Author: User
Tags datetime iso 8601 reset thread time and date tostring
Standard | string standard DateTime format string contains one of the format specifier characters in the following table. If the format specifier is not in the following table, a Run-time exception is thrown. If the format string is longer than a single character (even if the extra characters are blank), the format string is interpreted as a custom format string.

Note that the output string produced by these format specifiers is affected by the settings in the Regional Options control Panel. The computer's culture settings or date and time settings are different, and different output strings are generated.

The time and date delimiters displayed by the format string are defined by the DateSeparator and TimeSeparator characters associated with the DateTimeFormat property of the current culture. However, if the InvariantCulture is referenced by the "R", "s" and "U" specifiers, the characters associated with the DateSeparator and TimeSeparator characters do not change with the current culture.

The following table describes the standard format specifiers used to format DateTime objects.

Format specifier name description
The D Short Date pattern displays the pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread or the pattern defined by the specified format provider.
D Long Date mode displays the pattern defined by the Datetimeformatinfo.longdatepattern property associated with the current thread, or the pattern defined by the specified format provider.
The T short time mode displays the pattern defined by the Datetimeformatinfo.shorttimepattern property associated with the current thread, or the pattern defined by the specified format provider.
T long time mode displays the pattern defined by the Datetimeformatinfo.longtimepattern property associated with the current thread, or the pattern defined by the specified format provider.
F Full Date/time pattern (short time) displays a combination of a long date and a short time pattern, separated by a space.
F Full Date/time pattern (long time) displays the pattern defined by the Datetimeformatinfo.fulldatetimepattern property associated with the current thread or by the specified format provider.
G General Date/Time mode (short time) displays a combination of short date and short time patterns, separated by spaces.
G General Date/Time mode (long time) displays a combination of short date and long time patterns, separated by spaces.
The M or m month-day mode displays the pattern defined by the Datetimeformatinfo.monthdaypattern property associated with the current thread, or the pattern defined by the specified format provider.
The R or R RFC1123 mode displays the pattern defined by the Datetimeformatinfo.rfc1123pattern property associated with the current thread or by the specified format provider. This is the standard defined and the property is read-only, so it is always the same regardless of the culture used or the format provider that is provided. Property references the CultureInfo.InvariantCulture property and follows the custom mode "ddd, dd MMM yyyy HH:mm:ss G\MT". Note that "M" in "GMT" requires an escape character, so it is not interpreted. Formatting does not modify the value of DateTime, so you must adjust the value to GMT before formatting.
s sortable date/time pattern, conforming to ISO 8601 showing patterns defined by the Datetimeformatinfo.sortabledatetimepattern property associated with the current thread or defined by the specified format provider. Property refers to the CultureInfo.InvariantCulture property, which is formatted in accordance with the custom pattern "Yyyy-mm-ddthh:mm:ss".
The U-common sortable date/time pattern displays the pattern defined by the Datetimeformatinfo.universalsortabledatetimepattern property associated with the current thread or by the specified format provider. Because it is a defined standard and the property is read-only, the pattern is always the same regardless of the culture or format provider. The format follows the custom pattern "Yyyy-mm-dd HH:mm:ssZ". Format date and time without time zone conversion, so convert the local date and time to universal time before using the format specifier.
The U-common sortable date/time pattern displays the pattern defined by the Datetimeformatinfo.fulldatetimepattern property associated with the current thread or by the specified format provider. Note that the time displayed is universal, not local.
The Y or Y-year mode displays the pattern defined by the Datetimeformatinfo.yearmonthpattern property associated with the current thread, or the pattern defined by the specified format provider.
Any other single character unknown descriptor

The following example shows how a standard format string is used with a DateTime object.

[Visual Basic]
Dim dt as DateTime = DateTime.Now
Dim DFI as DateTimeFormatInfo = New DateTimeFormatInfo ()
Dim ci as CultureInfo = New CultureInfo ("De-de")

' Make-up a new custom DateTime-pattern, for demonstration.
DFi. Monthdaypattern = "Mm-mmmm, ddd-dddd"

' Use the DateTimeFormat ' culture associated
' With the current thread.

Console.WriteLine (dt. ToString ("D"))
Console.WriteLine (dt. ToString ("M"))

' Use the DateTimeFormat ' specific culture passed.
Console.WriteLine (dt. ToString ("D", CI))

' Use the ' Settings ' DateTimeFormatInfo object passed.
Console.WriteLine (dt. ToString ("M", DFI))

' Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo ("fr-be")
Console.WriteLine (dt. ToString ("D"))

[C #]
DateTime dt = DateTime.Now;
DateTimeFormatInfo DFI = new DateTimeFormatInfo ();
CultureInfo ci = new CultureInfo ("De-de");

Make up a new custom DateTime, for demonstration.
DFi. Monthdaypattern = "mm-mmmm, ddd-dddd";

Use the DateTimeFormat from the culture associated
With the current thread.
Console.WriteLine (dt. ToString ("D"));
Console.WriteLine (dt. ToString ("M"));

Use the DateTimeFormat from the specific culture passed.
Console.WriteLine (dt. ToString ("D", CI));

Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine (dt. ToString ("M", DFI));

Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo ("fr-be");
Console.WriteLine (dt. ToString ("D"));

Please see
Format Overview | Type of formatting | Date and time format string | Standard DateTime format string Output example

Send comments about this topic

©2001-2002 Microsoft Corporation. All rights reserved.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.