标准日期和时间格式字符串

更新:2007 年 11 月

标准日期和时间格式字符串使用单个标准格式说明符来定义通过格式设置操作产生的日期和时间值的文本表示形式。任何包含一个以上字符(包括空白)的日期和时间格式字符串都会作为自定义日期和时间格式字符串进行解释。

说明:

标准日期和时间格式字符串可以与 DateTimeDateTimeOffset 值一起使用。

标准格式字符串的工作原理

标准格式字符串只不过是自定义格式字符串的别名。使用别名引用自定义格式字符串的优点是:在别名保持固定不变的同时,自定义格式字符串自身可以变化。这很重要,因为日期和时间值的字符串表示形式通常会因区域性而异。例如,d 标准格式字符串指示应使用短日期模式显示日期和时间值。对于固定区域性,此模式为“MM/dd/yyyy”。对于 fr-FR 区域性,此模式为“dd/MM/yyyy”。对于 ja-JP 区域性,此模式为“yyyy/MM/dd”。

如果标准格式字符串映射到某个特定区域性的自定义格式字符串,则应用程序可定义该特定区域性并通过以下方式之一使用其自定义格式字符串:

  • 可使用默认的(或当前的)区域性。下面的示例使用当前区域性的短日期格式显示日期。在此情况下,当前区域性为 en-US。

    ' Display using current (en-us) culture's short date format
    Dim thisDate As Date = #03/15/2008#
    Console.WriteLine(thisDate.ToString("d"))     ' Displays 3/15/2008
    
    // 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
    
  • 可以传递一个 CultureInfo 表示区域性的对象,该区域性的格式设置将用于带有 IFormatProvider 参数的方法。下面的示例使用 pt-BR 区域性的短日期格式显示日期。

    ' Display using pt-BR culture's short date format
    Dim thisDate As Date = #03/15/2008#
    Dim culture As New CultureInfo("pt-BR")      
    Console.WriteLine(thisDate.ToString("d", culture))   ' Displays 15/3/2008
    
    // 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
    
  • 可以传递一个 DateTimeFormatInfo 对象,该对象向带有 IFormatProvider 参数的方法提供格式设置信息。下面的示例使用 hr-HR 区域性的 DateTimeFormatInfo 对象中的短日期格式显示日期。

    ' Display using date format information from hr-HR culture
    Dim thisDate As Date = #03/15/2008#
    Dim fmt As DateTimeFormatInfo = (New CultureInfo("hr-HR")).DateTimeFormat
    Console.WriteLine(thisDate.ToString("d", fmt))   ' Displays 15.3.2008
    
    // 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
    

某些情况下,标准格式字符串用作固定不变的较长自定义格式字符串的简便缩写。有四个标准格式字符串属于这一类别:O(或 o)、R(或 r)、s 和 u。这些字符串对应于由固定区域性定义的自定义格式字符串。通过这些字符串得到的日期和时间值的字符串表示形式在各个区域性中都应是相同的。下表提供了有关这四个标准日期和时间格式说明符的信息。

标准格式字符串

由 DateTimeFormatInfo.InvariantInfo 属性定义

自定义格式字符串

O 或 o

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

R 或 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'

标准日期和时间格式说明符

下表描述了标准日期和时间格式说明符。除非另行说明,否则,特定的标准日期和时间格式说明符将产生相同的字符串表示形式,这与它是与 DateTime 值还是 DateTimeOffset 值一起使用无关。

格式说明符

名称

说明

d

短日期模式

表示由当前的 ShortDatePattern 属性定义的自定义日期和时间格式字符串。例如,由固定区域性的 ShortDatePattern 属性返回的自定义格式字符串为“MM/dd/yyyy”。

下面的示例使用 d 格式说明符来显示日期和时间值。

D

长日期模式

表示由当前的 LongDatePattern 属性定义的自定义日期和时间格式字符串。例如,用于固定区域性的自定义格式字符串为“dddd, dd MMMM yyyy”。

下面的示例使用 D 格式说明符来显示日期和时间值。

f

完整日期/时间模式(短时间)

表示长日期 (D) 和短时间 (t) 模式的组合,由空格分隔。

下面的示例使用 f 格式说明符来显示日期和时间值。

F

完整日期/时间模式(长时间)

表示由当前的 FullDateTimePattern 属性定义的自定义日期和时间格式字符串。例如,用于固定区域性的自定义格式字符串为“dddd, dd MMMM yyyy HH:mm:ss”。

下面的示例使用 F 格式说明符来显示日期和时间值。

g

常规日期/时间模式(短时间)

表示短日期 (d) 和短时间 (t) 模式的组合,由空格分隔。下面的示例使用 g 格式说明符来显示日期和时间值。

G

常规日期/时间模式(长时间)

表示短日期 (d) 和长时间 (T) 模式的组合,由空格分隔。下面的示例使用 G 格式说明符来显示日期和时间值。

M, m

月日模式

表示由当前的 MonthDayPattern 属性定义的自定义日期和时间格式字符串。例如,用于固定区域性的自定义格式字符串为“MMMM dd”。下面的示例使用 G 格式说明符来显示日期和时间值。

O, o

往返日期/时间模式

表示使用保留时区信息的模式的自定义日期和时间格式字符串。对于 DateTime 值,此格式说明符设计用于在文本中将日期和时间值与 Kind 属性一起保留。随后将 ParseParseExact 与正确的 Kind 属性值一起使用可以对格式化的字符串进行反向分析。

对于 DateTime 值,自定义格式字符串为“yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK”,而对于 DateTimeOffset 值,自定义格式字符串为“yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz”。在此字符串中,分隔各个字符(例如连字线、冒号和字母“T”)的撇号对指示各个字符是不能更改的文本。撇号本身不会出现在输出字符串中。

此说明符的模式反映了已制定的标准 (ISO 8601)。因此,无论所使用的区域性或所提供的格式提供程序是什么,它总是相同的。传递给 ParseParseExact 方法的字符串必须与此定义格式模式完全符合,否则,将引发 FormatException

当使用此标准格式说明符时,格式设置或分析操作始终使用固定区域性。

下面的示例使用 o 格式说明符在美国太平洋标准时区中的系统上显示 DateTimeDateTimeOffset 值。

R, r

RFC1123 模式

表示由 DateTimeFormatInfo.RFC1123Pattern 属性定义的自定义日期和时间格式字符串。该模式反映了已制定的标准,并且该属性是只读的。因此,无论所使用的区域性或所提供的格式提供程序是什么,它总是相同的。定义格式字符串为“ddd, dd MMM yyyy HH':'mm':'ss 'GMT'”。

当使用此标准格式说明符时,格式设置或分析操作始终使用固定区域性。

格式设置不会修改正在设置格式的 DateTimeDateTimeOffset 对象的值。因此,应用程序在使用此格式模式之前必须将该值转换为协调世界时 (UTC)。

下面的示例使用 r 格式说明符在美国太平洋标准时区中的系统上显示 DateTimeDateTimeOffset 值。

s

可排序的日期/时间模式;符合 ISO 8601

表示由 DateTimeFormatInfo.SortableDateTimePattern 属性定义的自定义日期和时间格式字符串。该模式反映了已制定的标准,并且该属性是只读的。因此,无论所使用的区域性或所提供的格式提供程序是什么,它总是相同的。自定义格式字符串为“yyyy'-'MM'-'dd'T'HH':'mm':'ss”。

当使用此标准格式说明符时,格式设置或分析操作始终使用固定区域性。

下面的示例使用 s 格式说明符在美国太平洋标准时区中的系统上显示 DateTimeDateTimeOffset 值。

t

短时间模式

表示由当前的 ShortTimePattern 属性定义的自定义日期和时间格式字符串。例如,用于固定区域性的自定义格式字符串为“HH:mm”。

下面的示例使用 t 格式说明符来显示日期和时间值。

T

长时间模式

表示由当前的 LongTimePattern 属性定义的自定义日期和时间格式字符串。例如,用于固定区域性的自定义格式字符串为“HH:mm:ss”。

下面的示例使用 T 格式说明符来显示日期和时间值。

u

通用的可排序日期/时间模式

表示由 DateTimeFormatInfo.UniversalSortableDateTimePattern 属性定义的自定义日期和时间格式字符串。该模式反映了已制定的标准,并且该属性是只读的。因此,无论所使用的区域性或所提供的格式提供程序是什么,它总是相同的。自定义格式字符串为“yyyy'-'MM'-'dd HH':'mm':'ss'Z'”。

当使用此标准格式说明符时,格式设置或分析操作始终使用固定区域性。

格式设置不会为日期和时间对象转换时区。因此,应用程序在使用此格式说明符之前必须将日期和时间转换为协调世界时 (UTC)。

下面的示例使用 u 格式说明符来显示日期和时间值。

U

通用完整日期/时间模式

表示由当前的 FullDateTimePattern 属性定义的自定义日期和时间格式字符串。

此模式与 F 模式相同。但是,格式设置将作用于等于 DateTime 值的 UTC。

DateTimeOffset 类型不支持 U 格式说明符。如果使用 U 格式说明符来设置 DateTimeOffset 值的格式,则将引发 FormatException

下面的示例使用 T 格式说明符来显示日期和时间值。

Y, y

年月模式

表示由当前的 YearMonthPattern 属性定义的自定义日期和时间格式字符串。例如,用于固定区域性的自定义格式字符串为“yyyy MMMM”。

下面的示例使用 y 格式说明符来显示日期和时间值。

任何其他单个字符

(未知说明符)

引发运行时 FormatException

控制面板设置

控制面板中的区域和语言选项设置会影响格式设置操作产生的结果字符串。这些设置用于初始化与当前线程区域性关联的 DateTimeFormatInfo 对象,当前线程区域性提供用于控制格式设置的值。使用不同设置的计算机会产生不同的结果字符串。

此外,如果使用 CultureInfo.CultureInfo(String) 构造函数实例化一个新的 CultureInfo 对象以表示与当前的系统区域性相同的区域性,则通过控制面板中的“区域和语言选项”建立的任何自定义都将应用到新的 CultureInfo 对象。可以使用 CreateSpecificCulture 方法来创建不会反映系统的自定义项的 CultureInfo 对象。

DateTimeFormatInfo 属性

当前 DateTimeFormatInfo 对象的属性会影响格式设置,该对象由当前线程区域性隐式提供或由格式设置方法的 IFormatProvider 参数显式提供。对于 IFormatProvider 参数,应用程序应指定一个表示区域性的 CultureInfo 对象或表示特定区域性的日期和时间格式设置约定的 DateTimeFormatInfo 对象。许多标准日期和时间格式说明符是由当前的 DateTimeFormatInfo 对象的属性定义的格式设置模式的别名。应用程序通过更改相应 DateTimeFormatInfo 属性的相应日期和时间格式模式,可以更改由某些标准日期和时间格式说明符产生的结果。

使用标准格式字符串

下面的代码演示如何将标准格式字符串与 DateTime 值一起使用。

Dim dt As DateTime = DateTime.Now
Dim dfi As DateTimeFormatInfo = New DateTimeFormatInfo()
Dim ci As CultureInfo = 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.
Dim culter As New CultureInfo("en-US")
Dim myDateTime As DateTime 
myDateTime = DateTime.ParseExact("Tuesday, April 10, 2001", "D", culter)
Console.WriteLine(myDateTime.ToString("D"))
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"));

示例

下面的示例使用线程的当前区域性、指定的区域性以及所有标准日期和时间格式说明符设置 DateTime 对象的格式。

' 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.

Imports System
Imports System.Globalization
Imports System.Threading

Class Sample
    Public Shared Sub Main() 
        Dim msgShortDate As String = "(d) Short date: . . . . . . . "
        Dim msgLongDate As String  = "(D) Long date:. . . . . . . . "
        Dim msgShortTime As String = "(t) Short time: . . . . . . . "
        Dim msgLongTime As String  = "(T) Long time:. . . . . . . . "
        Dim msgFullDateShortTime As String = _
                                     "(f) Full date/short time: . . "
        Dim msgFullDateLongTime As String = _
                                     "(F) Full date/long time:. . . "
        Dim msgGeneralDateShortTime As String = _
                                     "(g) General date/short time:. "
        Dim msgGeneralDateLongTime As String = _
                                     "(G) General date/long time (default):" & vbCrLf & _
                                     "    . . . . . . . . . . . . . "
        Dim msgMonth As String     = "(M) Month:. . . . . . . . . . "
        Dim msgRFC1123 As String   = "(R) RFC1123:. . . . . . . . . "
        Dim msgSortable As String  = "(s) Sortable: . . . . . . . . "
        Dim msgUniSortInvariant As String = _
                                     "(u) Universal sortable (invariant):" & vbCrLf & _
                                     "    . . . . . . . . . . . . . "
        Dim msgUniFull As String   = "(U) Universal full date/time: "
        Dim msgYear As String      = "(Y) Year: . . . . . . . . . . "

        Dim msgRoundtripLocal As String         = "(o) Roundtrip (local):. . . . "
        Dim msgRoundtripUTC As String           = "(o) Roundtrip (UTC):. . . . . "
        Dim msgRoundtripUnspecified As String   = "(o) Roundtrip (Unspecified):. "


        Dim msg1 As String = "Use ToString(String) and the current thread culture." & vbCrLf
        Dim msg2 As String = "Use ToString(String, IFormatProvider) and a specified culture." & vbCrLf
        Dim msgCulture As String   = "Culture:"
        Dim msgThisDate As String  = "This date and time: {0}" & vbCrLf

        Dim thisDate As DateTime = DateTime.Now
        Dim  utcDate As DateTime = thisDate.ToUniversalTime()
        Dim unspecifiedDate As DateTime = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified)
        Dim ci As CultureInfo

        ' Format the current date and time in various ways.
        Console.Clear()
        Console.WriteLine("Standard DateTime Format Specifiers:" & vbCrLf)
        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}" & vbCrLf, 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}" & vbCrLf, 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()

    End Sub 'Main
End Class 'Sample
'
'This code example produces the following results:
'
'Standard DateTime Format Specifiers:
'
'This date and time: 4/17/2006 2:29: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:29 PM
'(T) Long time:. . . . . . . . 2:29:09 PM
'(f) Full date/short time: . . Monday, April 17, 2006 2:29 PM
'(F) Full date/long time:. . . Monday, April 17, 2006 2:29:09 PM
'(g) General date/short time:. 4/17/2006 2:29 PM
'(G) General date/long time (default):
'    . . . . . . . . . . . . . 4/17/2006 2:29:09 PM
'(M) Month:. . . . . . . . . . April 17
'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT
'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09
'(u) Universal sortable (invariant):
'    . . . . . . . . . . . . . 2006-04-17 21:29:09Z
'(U) Universal full date/time: Monday, April 17, 2006 9:29:09 PM
'(Y) Year: . . . . . . . . . . April, 2006
'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00
'(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z
'(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:29
'(T) Long time:. . . . . . . . 14:29:09
'(f) Full date/short time: . . Montag, 17. April 2006 14:29
'(F) Full date/long time:. . . Montag, 17. April 2006 14:29:09
'(g) General date/short time:. 17.04.2006 14:29
'(G) General date/long time (default):
'    . . . . . . . . . . . . . 17.04.2006 14:29:09
'(M) Month:. . . . . . . . . . 17 April
'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT
'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09
'(u) Universal sortable (invariant):
'    . . . . . . . . . . . . . 2006-04-17 21:29:09Z
'(U) Universal full date/time: Montag, 17. April 2006 21:29:09
'(Y) Year: . . . . . . . . . . April 2006
'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00
'(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z
'(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
// 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

*/
// 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^ 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 = 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(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 = 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(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: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 full date/time: 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 full date/time: 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

*/

请参见

概念

格式化概述

日期与时间格式字符串

自定义日期和时间格式字符串

其他资源

格式化类型