共用方式為


標準日期和時間格式字串

更新:2007 年 11 月

標準日期和時間格式字串使用單一標準格式規範,來定義由格式化作業所產生之日期和時間值的文字表示。任何包含一個以上字元 (包含泛空白字元 (White Space)) 的日期和時間格式字串都會解譯為自訂日期和時間格式字串

注意事項:

標準日期和時間格式字串可以與 DateTimeDateTimeOffset 值搭配使用。

標準格式字串的運作方式

標準格式字串只是自訂格式字串的別名。使用別名來表示自訂格式字串的好處是,儘管別名保持不變,自訂格式字串本身則可有變化。這點非常重要,因為日期和時間值的字串表示通常會因文化特性 (Culture) 而不同。例如,d 標準格式字串表示日期和時間值將使用簡短日期模式顯示。在不因文化特性而異 (Invariant Culture) 的情況下,此模式為 "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 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008#
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
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

完整日期模式

表示由目前 LongDatePattern 屬性所定義的自訂日期和時間格式字串。例如,不因文化特性而異的自訂格式字串為 "dddd, dd MMMM yyyy"。

下列範例使用 D 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008#
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
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

完整可排序日期/時間模式 (簡短時間)

表示完整日期 (D) 和簡短時間 (t) 模式的組合,兩者以空格來分隔。

下列範例使用 f 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
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
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

完整可排序日期/時間模式 (完整時間)

表示由目前 FullDateTimePattern 屬性所定義的自訂日期和時間格式字串。例如,不因文化特性而異的自訂格式字串為 "dddd, dd MMMM yyyy HH:mm:ss"。

下列範例使用 F 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
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
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

一般可排序日期/時間模式 (簡短時間)

表示簡短日期 (d) 和簡短時間 (t) 模式的組合,兩者以空格來分隔。下列範例使用 g 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
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
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

一般可排序日期/時間模式 (完整時間)

表示簡短日期 (d) 和完整時間 (T) 模式的組合,兩者以空格來分隔。下列範例使用 G 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
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
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

月日模式

表示由目前 MonthDayPattern 屬性所定義的自訂日期和時間格式字串。例如,不因文化特性而異的自訂格式字串為 "MMMM dd"。下列範例使用 G 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("m", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays April 10
Console.WriteLine(date1.ToString("m", _
CultureInfo.CreateSpecificCulture("ms-MY")))
' Displays 10 April
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

來回日期/時間模式

表示使用保存時區資訊的模式的自訂日期和時間格式字串。若為 DateTime 值,此格式規範是設計來以純文字保存日期和時間值以及 Kind 屬性。接著可以使用 ParseParseExact 搭配正確的 Kind 屬性值,將格式化的字串剖析回原始字串。

DateTime 值的自訂格式字串為 "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK",而 DateTimeOffset 值的自訂格式字串為 "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz"。在此字串中,括住單個字元 (如連字號、冒號和字母 "T") 的一組所有格符號表示該字元是不可變更的常值 (Literal)。所有格符號本身不會出現在輸出字串中。

這個規範的模式反映的是一個已定義的標準 (ISO 8601)。因此,不論所使用的文化特性或提供的格式提供者為何,它一定會是相同的。傳遞至 ParseParseExact 方法的字串必須完全符合此自訂格式模式,否則就會擲回 FormatException

使用這個標準格式規範時,格式或剖析作業一律使用不因文化特性而異。

下列範例會使用 o 格式規範,顯示美國太平洋標準時間系統上的 DateTimeDateTimeOffset 值。

Dim date1 As Date = #4/10/2008 6:30AM#
Dim dateOffset As 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
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

R、r

RFC1123 模式

表示由 DateTimeFormatInfo.RFC1123Pattern 屬性所定義的自訂日期和時間格式字串。此模式反映已定義的標準,且屬性為唯讀。因此,不論所使用的文化特性或提供的格式提供者為何,它一定會是相同的。自訂格式字串為 "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"。

使用這個標準格式規範時,格式或剖析作業一律使用不因文化特性而異。

格式化不會修改進行格式化之 DateTimeDateTimeOffset 物件的值。因此,應用程式必須在使用此格式模式之前將該值轉換為 Coordinated Universal Time (UTC)。

下列範例會使用 r 格式規範,顯示美國太平洋標準時間系統上的 DateTimeDateTimeOffset 值。

Dim date1 As Date = #4/10/2008 6:30AM#
Dim dateOffset As 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
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

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

表示由 DateTimeFormatInfo.SortableDateTimePattern 屬性所定義的自訂日期和時間格式字串。此模式反映已定義的標準,且屬性為唯讀。因此,不論所使用的文化特性或提供的格式提供者為何,它一定會是相同的。自訂格式字串為 "yyyy'-'MM'-'dd'T'HH':'mm':'ss"。

使用這個標準格式規範時,格式或剖析作業一律使用不因文化特性而異。

下列範例會使用 s 格式規範,顯示美國太平洋標準時間系統上的 DateTimeDateTimeOffset 值。

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("s"))
' Displays 2008-04-10T06:30:00
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("s"));
// Displays 2008-04-10T06:30:00

t

簡短時間模式

表示由目前 ShortTimePattern 屬性所定義的自訂日期和時間格式字串。例如,不因文化特性而異的自訂格式字串為 "HH:mm"。

下列範例使用 t 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
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
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

完整時間模式

表示由目前 LongTimePattern 屬性所定義的自訂日期和時間格式字串。例如,不因文化特性而異的自訂格式字串為 "HH:mm:ss"。

下列範例使用 T 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
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
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

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

表示由 DateTimeFormatInfo.UniversalSortableDateTimePattern 屬性所定義的自訂日期和時間格式字串。此模式反映已定義的標準,且屬性為唯讀。因此,不論所使用的文化特性或提供的格式提供者為何,它一定會是相同的。自訂格式字串為 "yyyy'-'MM'-'dd HH':'mm':'ss'Z'"。

使用這個標準格式規範時,格式或剖析作業一律使用不因文化特性而異。

格式化不會轉換日期和時間物件的時區。因此,應用程式必須在使用此格式規範之前,將日期和時間轉換為 Coordinated Universal Time (UTC)。

下列範例使用 u 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToUniversalTime.ToString("u"))
' Displays 2008-04-10 13:30:00Z
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToUniversalTime().ToString("u"));
// Displays 2008-04-10 13:30:00Z

U

通用的完整日期/時間模式

表示由目前 FullDateTimePattern 屬性所定義的自訂日期和時間格式字串。

此模式與 F 模式相同。不過,格式化作業是針對相當於 DateTime 值的 UTC 所進行。

DateTimeOffset 型別不支援 U 格式規範,如果使用它來格式化 DateTimeOffset,就會擲回 FormatException

下列範例使用 T 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
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
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

年月模式

表示由目前 YearMonthPattern 屬性所定義的自訂日期和時間格式字串。例如,不因文化特性而異的自訂格式字串為 "yyyy MMMM"。

下列範例使用 y 格式規範來顯示日期和時間值。

Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("Y", CultureInfo.CreateSpecificCulture("en-US")))
' Displays April, 2008
Console.WriteLine(date1.ToString("y", CultureInfo.CreateSpecificCulture("af-ZA")))
' Displays April 2008
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

任何其他單一字元

(未知的規範)

擲回執行階段 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

*/

請參閱

概念

格式化概觀

日期和時間格式字串

自訂日期和時間格式字串

其他資源

格式化型別