標準 DateTime 格式字符串

 

 

標準 DateTime 格式字符串包含一個標準 DateTime 格式說明符字符,該字符表示自定義 DateTime 格式字符串。格式字符串最終定義由格式化操作產生的 DateTime 對象的文本表示形式。請注意,任何包含一個以上字母字符(包括空白)的 DateTime 格式字符串都被解釋爲自定義 DateTime 格式字符串。

標準 DateTime 格式說明符

下表描述了標準 DateTime 格式說明符。有關各格式說明符所產生輸出的示例,請參見標準 DateTime 格式字符串輸出示例

格式說明符 名稱 說明

d

短日期模式

表示由當前 ShortDatePattern 屬性定義的自定義 DateTime 格式字符串。

例如,用於固定區域性的自定義格式字符串爲“MM/dd/yyyy”。

D

長日期模式

表示由當前 LongDatePattern 屬性定義的自定義 DateTime 格式字符串。

例如,用於固定區域性的自定義格式字符串爲“dddd, dd MMMM yyyy”。

f

完整日期/時間模式(短時間)

表示長日期 (D) 和短時間 (t) 模式的組合,由空格分隔。

F

完整日期/時間模式(長時間)

表示由當前 FullDateTimePattern 屬性定義的自定義 DateTime 格式字符串。

例如,用於固定區域性的自定義格式字符串爲“dddd, dd MMMM yyyy HH:mm:ss”。

g

常規日期/時間模式(短時間)

表示短日期 (d) 和短時間 (t) 模式的組合,由空格分隔。

G

常規日期/時間模式(長時間)

表示短日期 (d) 和長時間 (T) 模式的組合,由空格分隔。

M 或 m

月日模式

表示由當前 MonthDayPattern 屬性定義的自定義 DateTime 格式字符串。

例如,用於固定區域性的自定義格式字符串爲“MMMM dd”。

o

往返日期/時間模式

表示使用保留時區信息的模式的自定義 DateTime 格式字符串。該模式專用於往返 DateTime 格式(包括文本形式的 Kind 屬性)。隨後將 ParseParseExact 與正確的 Kind 屬性值一起使用可以對格式化的字符串進行反向分析。

自定義格式字符串爲“yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK”。

用於此說明符的模式是定義的標準。因此,無論所使用的區域性或所提供的格式提供程序是什麼,它總是相同的。

R 或 r

RFC1123 模式

表示由當前 RFC1123Pattern 屬性定義的自定義 DateTime 格式字符串。該模式是定義的標準,並且屬性是隻讀的。因此,無論所使用的區域性或所提供的格式提供程序是什麼,它總是相同的。

定義格式字符串爲“ddd, dd MMM yyyy HH':'mm':'ss 'GMT'”。

格式化不會修改正在格式化的 DateTime 對象的值。因此,應用程序在使用此格式說明符之前必須將該值轉換爲協調世界時 (UTC)。

s

可排序的日期/時間模式;符合 ISO 8601

表示由當前 SortableDateTimePattern 屬性定義的自定義 DateTime 格式字符串。此模式是定義的標準,並且屬性是隻讀的。因此,無論所使用的區域性或所提供的格式提供程序是什麼,它總是相同的。

自定義格式字符串爲“yyyy'-'MM'-'dd'T'HH':'mm':'ss”。

t

短時間模式

表示由當前 ShortTimePattern 屬性定義的自定義 DateTime 格式字符串。

例如,用於固定區域性的自定義格式字符串爲“HH:mm”。

T

長時間模式

表示由當前 LongTimePattern 屬性定義的自定義 DateTime 格式字符串。

例如,用於固定區域性的自定義格式字符串爲“HH:mm:ss”。

u

通用的可排序日期/時間模式

表示由當前 UniversalSortableDateTimePattern 屬性定義的自定義 DateTime 格式字符串。此模式是定義的標準,並且屬性是隻讀的。因此,無論所使用的區域性或所提供的格式提供程序是什麼,它總是相同的。

自定義格式字符串爲“yyyy'-'MM'-'dd HH':'mm':'ss'Z'”。

格式化日期和時間時不進行時區轉換。因此,應用程序在使用此格式說明符之前必須將本地日期和時間轉換爲協調世界時 (UTC)。

U

通用的可排序日期/時間模式

表示由當前 FullDateTimePattern 屬性定義的自定義 DateTime 格式字符串。

此模式與完整日期/長時間 (F) 模式相同。但是,格式化將作用於等效於正在格式化的 DateTime 對象的協調世界時 (UTC)。

Y 或 y

年月模式

表示由當前 YearMonthPattern 屬性定義的自定義 DateTime 格式字符串。

例如,用於固定區域性的自定義格式字符串爲“yyyy MMMM”。

任何其他單個字符

(未知說明符)

未知說明符將引發運行時格式異常。

控制面板設置

控制面板中“區域和語言選項”項中的設置會影響由格式化操作產生的結果字符串。這些設置用於初始化與當前線程區域性關聯的 DateTimeFormatInfo 對象,當前線程區域性將提供用於控制格式設置的值。使用不同設置的計算機會生成不同的結果字符串。

DateTimeFormatInfo 屬性

格式化受當前 DateTimeFormatInfo 對象的屬性影響,其由當前線程區域性隱式提供或由調用格式化的方法的 IFormatProvider 參數顯示提供。爲 IFormatProvider 參數指定一個表示區域性的 CultureInfo 對象或指定一個 DateTimeFormatInfo 對象。

許多標準 DateTime 格式說明符是由當前 DateTimeFormatInfo 對象的屬性定義的格式化模式的別名。因此,應用程序通過更改相應的 DateTimeFormatInfo 屬性可以改變由某些標準 DateTime 格式說明符產生的結果。

使用標準格式字符串

下面的代碼段演示如何一起使用標準格式字符串和 DateTime 對象。

Visual Basic
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 格式說明符對 DateTime 對象進行格式化。

Visual Basic
' 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 msgUniSort As String   = "(U) Universal sortable: . . . "
        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(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 = 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(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()
    
    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 sortable: . . . 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 sortable: . . . 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 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 = 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(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 = 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(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: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 sortable: . . . 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 sortable: . . . 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^ 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

*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章