中国加油

我也加油

导航

Standard Date and Time Format Strings

 

Standard Date and Time Format Strings

Updated: October 2008

A standard date and time format string uses a single standard format specifier to define the text representation of a date and time value that is produced by a formatting operation. Any date and time format string that contains more than one character, including white space, is interpreted as a custom date and time format string.

Note:

Standard date and time format strings can be used with both DateTime and DateTimeOffset values.

 How Standard Format Strings Work

A standard format string is simply an alias for a custom format string. The advantage of using an alias to refer to a custom format string is that, while the alias remains invariant, the custom format string itself can vary. This is important because the string representations of date and time values typically vary by culture. For example, the d standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".

If a standard format string maps to a particular culture's custom format string, your application can define the specific culture whose custom format strings are used in one of these ways:

  • You can use the default (or current) culture. The following example displays a date using the current culture's short date format. In this case, the current culture is en-US.

C#

Copy Code

// Display using current (en-us) culture's short date format

DateTime thisDate = new DateTime(2008, 3, 15);

Console.WriteLine(thisDate.ToString("d"));           // Displays 3/15/2008

 

  • You can pass a CultureInfo object representing the culture whose formatting is to be used to a method with an IFormatProvider parameter. The following example displays a date using the short date format of the pt-BR culture.

C#

Copy Code

// Display using pt-BR culture's short date format

DateTime thisDate = new DateTime(2008, 3, 15);

CultureInfo culture = new CultureInfo("pt-BR");     

Console.WriteLine(thisDate.ToString("d", culture)); // Displays 15/3/2008

 

C#

Copy Code

// Display using date format information from hr-HR culture

DateTime thisDate = new DateTime(2008, 3, 15);

DateTimeFormatInfo fmt = (new CultureInfo("hr-HR")).DateTimeFormat;

Console.WriteLine(thisDate.ToString("d", fmt));      // Displays 15.3.2008

 

In some cases, the standard format string serves as a convenient abbreviation for a longer custom format string that is invariant. Four standard format strings fall into this category: O (or o), R (or r), s, and u. These strings correspond to custom format strings defined by the invariant culture. They produce string representations of date and time values that are intended to be identical across cultures. The following table provides information on these four standard date and time format strings.

Standard format string

Defined by DateTimeFormatInfo.InvariantInfo property

Custom format string

O or o

none

yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzz

R or r

RFC1123Pattern

ddd, dd MMM yyyy HH':'mm':'ss 'GMT'

s

SortableDateTimePattern

yyyy'-'MM'-'dd'T'HH':'mm':'ss

u

UniversalSortableDateTimePattern

yyyy'-'MM'-'dd HH':'mm':'ss'Z'

 Standard Date and Time Format Specifiers

The following table describes the standard date and time format specifiers. Unless otherwise noted, a particular standard date and time format specifier produces an identical string representation regardless of whether it is used with a DateTime value or a DateTimeOffset value.

Format specifier

Name

Description

d

Short date pattern

Represents a custom date and time format string defined by the current ShortDatePattern property. For example, the custom format string returned by the ShortDatePattern property of the invariant culture is "MM/dd/yyyy".

The following example uses the d format specifier to display a date and time value.

CultureInfo.CreateSpecificCulture(

      CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

C#

Copy Code

DateTime date1 = new DateTime(2008,4, 10);

Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo));

// Displays 04/10/2008

Console.WriteLine(date1.ToString("d",

                  CultureInfo.CreateSpecificCulture("en-US")));

// Displays 4/10/2008                      

Console.WriteLine(date1.ToString("d",

                  CultureInfo.CreateSpecificCulture("en-NZ")));

// Displays 10/04/2008                      

Console.WriteLine(date1.ToString("d",

                  CultureInfo.CreateSpecificCulture("de-DE")));

// Displays 10.04.2008                      

D

Long date pattern

Represents a custom date and time format string defined by the current LongDatePattern property. For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy".

The following example uses the D format specifier to display a date and time value.

CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10);

Console.WriteLine(date1.ToString("D",

                  CultureInfo.CreateSpecificCulture("en-US")));

// Displays Thursday, April 10, 2008                       

Console.WriteLine(date1.ToString("D",

                  CultureInfo.CreateSpecificCulture("pt-BR")));

// Displays quinta-feira, 10 de abril de 2008                       

Console.WriteLine(date1.ToString("D",

                  CultureInfo.CreateSpecificCulture("es-MX")));

// Displays jueves, 10 de abril de 2008                       

f

Full date/time pattern (short time)

Represents a combination of the long date (D) and short time (t) patterns, separated by a space.

The following example uses the f format specifier to display a date and time value.

CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("f",

                  CultureInfo.CreateSpecificCulture("en-US")));

// Displays Thursday, April 10, 2008 6:30 AM                       

Console.WriteLine(date1.ToString("f",

                  CultureInfo.CreateSpecificCulture("fr-FR")));

// Displays jeudi 10 avril 2008 06:30                      

F

Full date/time pattern (long time)

Represents a custom date and time format string defined by the current FullDateTimePattern property. For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy HH:mm:ss".

The following example uses the F format specifier to display a date and time value.

CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("F",

                  CultureInfo.CreateSpecificCulture("en-US")));

// Displays Thursday, April 10, 2008 6:30:00 AM                       

Console.WriteLine(date1.ToString("F",

                  CultureInfo.CreateSpecificCulture("fr-FR")));

// Displays jeudi 10 avril 2008 06:30:00                      

g

General date/time pattern (short time)

Represents a combination of the short date (d) and short time (t) patterns, separated by a space. The following example uses the g format specifier to display a date and time value.

DateTimeFormatInfo.InvariantInfo))

CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("g",

                  DateTimeFormatInfo.InvariantInfo));

// Displays 04/10/2008 06:30                     

Console.WriteLine(date1.ToString("g",

                  CultureInfo.CreateSpecificCulture("en-us")));

// Displays 4/10/2008 6:30 AM                       

Console.WriteLine(date1.ToString("g",

                  CultureInfo.CreateSpecificCulture("fr-BE")));

// Displays10/04/2008 6:30                       

G

General date/time pattern (long time)

Represents a combination of the short date (d) and long time (T) patterns, separated by a space. The following example uses the G format specifier to display a date and time value.

DateTimeFormatInfo.InvariantInfo))

CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("G",

                  DateTimeFormatInfo.InvariantInfo));

// Displays 04/10/2008 06:30:00

Console.WriteLine(date1.ToString("G",

                  CultureInfo.CreateSpecificCulture("en-us")));

// Displays 4/10/2008 6:30:00 AM                       

Console.WriteLine(date1.ToString("G",

                  CultureInfo.CreateSpecificCulture("nl-BE")));

// Displays 10/04/2008 6:30:00                      

M, m

Month day pattern

Represents a custom date and time format string defined by the current MonthDayPattern property. For example, the custom format string for the invariant culture is "MMMM dd". The following example uses the "M" format specifier to display a date and time value.

CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("m",

                  CultureInfo.CreateSpecificCulture("en-us")));

// Displays April 10                       

Console.WriteLine(date1.ToString("m",

                  CultureInfo.CreateSpecificCulture("ms-MY")));

// Displays 10 April                      

O, o

Round-trip date/time pattern

Represents a custom date and time format string using a pattern that preserves time zone information. For DateTime values, this format specifier is designed to preserve date and time values along with the Kind property in text. The formatted string can be parsed back using the DateTime..::.Parse(String, IFormatProvider, DateTimeStyles) or ParseExact method if the styles parameter is set to DateTimeStyles..::.RoundtripKind.

The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" for DateTime values and "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz" for DateTimeOffset values. In this string, the pairs of apostrophes that delimit individual characters, such as the hyphens, the colons, and the letter "T", indicate that the individual character is a literal that cannot be changed. The apostrophes themselves do not appear in the output string.

The pattern for this specifier reflects a defined standard (ISO 8601). Therefore, it is always the same regardless of the culture used or the format provider supplied. Strings that are passed to the Parse or ParseExact method must conform exactly to this custom format pattern, or a FormatException is thrown.

When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.

The following example uses the o format specifier to display a DateTime and a DateTimeOffset value on a system in the U.S. Pacific Standard Time zone.

                    

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

DateTimeOffset dateOffset = new DateTimeOffset(date1,

                            TimeZoneInfo.Local.GetUtcOffset(date1));

Console.WriteLine(date1.ToString("o"));

// Displays 2008-04-10T06:30:00.0000000                       

Console.WriteLine(dateOffset.ToString("o"));

// Displays 2008-04-10T06:30:00.0000000-07:00                      

 

The following example uses the o format specifier to create a formatted string, then restores the original date and time value by calling a date and time Parse method.

newDate, newDate.Kind)

newDate, newDate.Kind)

             newDate, newDate.Kind)

Round-tripped 4/10/2008 6:30:00 AM Local to 4/10/2008 6:30:00 AM Local.

Round-tripped 4/12/2008 9:30:00 AM Utc to 4/12/2008 9:30:00 AM Utc.

Round-tripped 4/13/2008 12:30:00 PM Unspecified to 4/13/2008 12:30:00 PM Unspecified.

Round-tripped 4/12/2008 9:30:00 AM -08:00 to 4/12/2008 9:30:00 AM -08:00.

C#

Copy Code

// Round-trip DateTime values.

DateTime originalDate, newDate;

string dateString;

// Round-trip a local time.

originalDate = DateTime.SpecifyKind(new DateTime(2008, 4, 10, 6, 30, 0), DateTimeKind.Local);

dateString = originalDate.ToString("o");

newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);

Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind,

                  newDate, newDate.Kind);

// Round-trip a UTC time.

originalDate = DateTime.SpecifyKind(new DateTime(2008, 4, 12, 9, 30, 0), DateTimeKind.Utc);                 

dateString = originalDate.ToString("o");

newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);

Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind,

                  newDate, newDate.Kind);

// Round-trip time in an unspecified time zone.

originalDate = DateTime.SpecifyKind(new DateTime(2008, 4, 13, 12, 30, 0), DateTimeKind.Unspecified);                 

dateString = originalDate.ToString("o");

newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);

Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind,

                  newDate, newDate.Kind);

 

// Round-trip a DateTimeOffset value.

DateTimeOffset originalDTO = new DateTimeOffset(2008, 4, 12, 9, 30, 0, new TimeSpan(-8, 0, 0));

dateString = originalDTO.ToString("o");

DateTimeOffset newDTO = DateTimeOffset.Parse(dateString, null, DateTimeStyles.RoundtripKind);

Console.WriteLine("Round-tripped {0} to {1}.", originalDTO, newDTO);

// The example displays the following output:

//    Round-tripped 4/10/2008 6:30:00 AM Local to 4/10/2008 6:30:00 AM Local.

//    Round-tripped 4/12/2008 9:30:00 AM Utc to 4/12/2008 9:30:00 AM Utc.

//    Round-tripped 4/13/2008 12:30:00 PM Unspecified to 4/13/2008 12:30:00 PM Unspecified.

//    Round-tripped 4/12/2008 9:30:00 AM -08:00 to 4/12/2008 9:30:00 AM -08:00.

R, r

RFC1123 pattern

Represents a custom date and time format string defined by the DateTimeFormatInfo..::.RFC1123Pattern property. The pattern reflects a defined standard and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied. The custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'".

When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.

Formatting does not modify the value of the DateTime or DateTimeOffset object that is being formatted. Therefore, the application must convert the value to Coordinated Universal Time (UTC) before using this format pattern.

The following example uses the r format specifier to display a DateTime and a DateTimeOffset value on a system in the U.S. Pacific Standard Time zone.

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

DateTimeOffset dateOffset = new DateTimeOffset(date1,

                            TimeZoneInfo.Local.GetUtcOffset(date1));

Console.WriteLine(date1.ToUniversalTime().ToString("r"));

// Displays Thu, 10 Apr 2008 13:30:00 GMT                      

Console.WriteLine(dateOffset.ToUniversalTime().ToString("r"));

// Displays Thu, 10 Apr 2008 13:30:00 GMT                       

s

Sortable date/time pattern; conforms to ISO 8601

Represents a custom date and time format string defined by the DateTimeFormatInfo..::.SortableDateTimePattern property. The pattern reflects a defined standard and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied. The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss".

When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.

The following example uses the s format specifier to display a DateTime and a DateTimeOffset value on a system in the U.S. Pacific Standard Time zone.

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("s"));

// Displays 2008-04-10T06:30:00                      

t

Short time pattern

Represents a custom date and time format string defined by the current ShortTimePattern property. For example, the custom format string for the invariant culture is "HH:mm".

The following example uses the t format specifier to display a date and time value.

CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("t",

                  CultureInfo.CreateSpecificCulture("en-us")));

// Displays 6:30 AM                       

Console.WriteLine(date1.ToString("t",

                  CultureInfo.CreateSpecificCulture("es-ES")));

// Displays 6:30                     

T

Long time pattern

Represents a custom date and time format string defined by the current LongTimePattern property. For example, the custom format string for the invariant culture is "HH:mm:ss".

The following example uses the T format specifier to display a date and time value.

CultureInfo.CreateSpecificCulture(

CultureInfo.CreateSpecificCulture(

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("T",

                  CultureInfo.CreateSpecificCulture("en-us")));

// Displays 6:30:00 AM                      

Console.WriteLine(date1.ToString("T",

                  CultureInfo.CreateSpecificCulture("es-ES")));

// Displays 6:30:00                     

u

Universal sortable date/time pattern

Represents a custom date and time format string defined by the DateTimeFormatInfo..::.UniversalSortableDateTimePattern property. The pattern reflects a defined standard and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied. The custom format string is "yyyy'-'MM'-'dd HH':'mm':'ss'Z'".

When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.

Formatting does not convert the time zone for the date and time object. Therefore, the application must convert a date and time to Coordinated Universal Time (UTC) before using this format specifier.

The following example uses the u format specifier to display a date and time value.

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToUniversalTime().ToString("u"));

// Displays 2008-04-10 13:30:00Z                      

U

Universal Full date/time pattern

Represents a custom date and time format string defined by the current FullDateTimePattern property.

The pattern is the same as the F pattern. However, formatting operates on the UTC that is equivalent to the DateTime value.

The U format specifier is not supported by the DateTimeOffset type and throws a FormatException if it is used to format a DateTimeOffset value.

The following example uses the T format specifier to display a date and time value.

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("U",

                  CultureInfo.CreateSpecificCulture("en-US")));

// Displays Thursday, April 10, 2008 1:30:00 PM                      

Console.WriteLine(date1.ToString("U",

                  CultureInfo.CreateSpecificCulture("sv-FI")));

// Displays den 10 april 2008 13:30:00                      

Y, y

Year month pattern

Represents a custom date and time format string defined by the current YearMonthPattern property. For example, the custom format string for the invariant culture is "yyyy MMMM".

The following example uses the y format specifier to display a date and time value.

C#

Copy Code

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);

Console.WriteLine(date1.ToString("Y",

                  CultureInfo.CreateSpecificCulture("en-US")));

// Displays April, 2008                      

Console.WriteLine(date1.ToString("y",

                  CultureInfo.CreateSpecificCulture("af-ZA")));

// Displays April 2008                       

Any other single character

(Unknown specifier)

Throws a runtime FormatException.

 Control Panel Settings

The regional and language option settings in Control Panel influence the result string produced by a formatting operation. These settings are used to initialize the DateTimeFormatInfo object associated with the current thread culture, which provides values used to govern formatting. Computers using different settings generate different result strings.

In addition, if the CultureInfo..::.CultureInfo(String) constructor is used to instantiate a new CultureInfo object that represents the same culture as the current system culture, any customizations established by the Regional and Language Options item in Control Panel will be applied to the new CultureInfo object. The CreateSpecificCulture method can be used to create a CultureInfo object that does not reflect a system's customizations.

 DateTimeFormatInfo Properties

Formatting is influenced by properties of the current DateTimeFormatInfo object, which is provided implicitly by the current thread culture or explicitly by the IFormatProvider parameter of the formatting method. For the IFormatProvider parameter, your application should specify a CultureInfo object, which represents a culture, or a DateTimeFormatInfo object, which represents a particular culture's date and time formatting conventions. Many of the standard date and time format specifiers are aliases for formatting patterns defined by properties of the current DateTimeFormatInfo object. Your application can change the result produced by some standard date and time format specifiers by changing the corresponding date and time format patterns of the corresponding DateTimeFormatInfo property.

 Using Standard Format Strings

The following code illustrates how to use the standard format strings with DateTime values.

C#

Copy Code

DateTime dt = DateTime.Now;

DateTimeFormatInfo dfi = new DateTimeFormatInfo();

CultureInfo ci = new CultureInfo("de-DE");

 

// Create a new custom DateTime pattern 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") );

 

// Use a CultureInfo with a format specifier to parse a string.

IFormatProvider culter = new CultureInfo("en-US");

DateTime myDateTime = DateTime.ParseExact("Tuesday, April 10, 2001", "D", culter);

Console.WriteLine(myDateTime.ToString("D"));

 Example

The following example formats a DateTime object using the thread current culture, a specified culture, and all the standard date and time format specifiers.

=

=

. . . . . . . . . . . . . "

=

=

=

                             . . . . . . . . . . . . . "

=

=

=

=

=

=

=

utcDate

Console.Clear()

Console.WriteLine(

Console.WriteLine(msgThisDate, thisDate)

Console.WriteLine(msg1)

ci = Thread.CurrentThread.CurrentCulture

Console.WriteLine(

Console.WriteLine(msgShortDate            &        thisDate.ToString(

Console.WriteLine(msgLongDate             &        thisDate.ToString(

Console.WriteLine(msgShortTime            &        thisDate.ToString(

Console.WriteLine(msgLongTime             &        thisDate.ToString(

Console.WriteLine(msgFullDateShortTime    &        thisDate.ToString(

Console.WriteLine(msgFullDateLongTime     &        thisDate.ToString(

Console.WriteLine(msgGeneralDateShortTime &        thisDate.ToString(

Console.WriteLine(msgGeneralDateLongTime &        thisDate.ToString(

Console.WriteLine(msgMonth                &        thisDate.ToString(

Console.WriteLine(msgRFC1123              &         utcDate.ToString(

Console.WriteLine(msgSortable             &        thisDate.ToString(

Console.WriteLine(msgUniSortInvariant     &         utcDate.ToString(

Console.WriteLine(msgUniFull              &        thisDate.ToString(

Console.WriteLine(msgYear                 &        thisDate.ToString(

Console.WriteLine(msgRoundtripLocal       &        thisDate.ToString(

Console.WriteLine(msgRoundtripUTC         &         utcDate.ToString(

 Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString(

Console.WriteLine()

Console.WriteLine(msg2)

ci =

Console.WriteLine(

Console.WriteLine(msgShortDate            &        thisDate.ToString(

Console.WriteLine(msgLongDate             &        thisDate.ToString(

Console.WriteLine(msgShortTime            &        thisDate.ToString(

Console.WriteLine(msgLongTime             &        thisDate.ToString(

Console.WriteLine(msgFullDateShortTime    &        thisDate.ToString(

Console.WriteLine(msgFullDateLongTime     &        thisDate.ToString(

Console.WriteLine(msgGeneralDateShortTime &        thisDate.ToString(

Console.WriteLine(msgGeneralDateLongTime &        thisDate.ToString(

Console.WriteLine(msgMonth                &        thisDate.ToString(

Console.WriteLine(msgRFC1123              &         utcDate.ToString(

Console.WriteLine(msgSortable             &        thisDate.ToString(

Console.WriteLine(msgUniSortInvariant     &         utcDate.ToString(

Console.WriteLine(msgUniFull              &        thisDate.ToString(

Console.WriteLine(msgYear                 &        thisDate.ToString(

Console.WriteLine(msgRoundtripLocal       &        thisDate.ToString(

Console.WriteLine(msgRoundtripUTC         &         utcDate.ToString(

Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString(

Console.WriteLine()

English (United States)

. . . . . . . . . . . . . 4/17/2006 2:29:09 PM

. . . . . . . . . . . . . 2006-04-17 21:29:09Z

German (Germany)

. . . . . . . . . . . . . 17.04.2006 14:29:09

. . . . . . . . . . . . . 2006-04-17 21:29:09Z

C#

Copy Code

// 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 System;

using System.Globalization;

using System.Threading;

 

class Sample

{

    public static void 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 msgUniFull =   "(U) Universal full date/time: ";

    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 = new 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(msgUniFull              +         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 = new 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(msgUniFull              +         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:22:48 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:22 PM

(T) Long time:. . . . . . . . 2:22:48 PM

(f) Full date/short time: . . Monday, April 17, 2006 2:22 PM

(F) Full date/long time:. . . Monday, April 17, 2006 2:22:48 PM

(g) General date/short time:. 4/17/2006 2:22 PM

(G) General date/long time (default):

    . . . . . . . . . . . . . 4/17/2006 2:22:48 PM

(M) Month:. . . . . . . . . . April 17

(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT

(s) Sortable: . . . . . . . . 2006-04-17T14:22:48

(u) Universal sortable (invariant):

    . . . . . . . . . . . . . 2006-04-17 21:22:48Z

(U) Universal full date/time: Monday, April 17, 2006 9:22:48 PM

(Y) Year: . . . . . . . . . . April, 2006

(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00

(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z

(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:22

(T) Long time:. . . . . . . . 14:22:48

(f) Full date/short time: . . Montag, 17. April 2006 14:22

(F) Full date/long time:. . . Montag, 17. April 2006 14:22:48

(g) General date/short time:. 17.04.2006 14:22

(G) General date/long time (default):

    . . . . . . . . . . . . . 17.04.2006 14:22:48

(M) Month:. . . . . . . . . . 17 April

(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT

(s) Sortable: . . . . . . . . 2006-04-17T14:22:48

(u) Universal sortable (invariant):

    . . . . . . . . . . . . . 2006-04-17 21:22:48Z

(U) Universal full date/time: Montag, 17. April 2006 21:22:48

(Y) Year: . . . . . . . . . . April 2006

(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00

(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z

(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

 

*/

 

posted on 2009-06-22 17:38  孔新  阅读(600)  评论(0编辑  收藏  举报