標準日期和時間格式字串Standard Date and Time Format Strings

標準日期和時間格式字串使用單一格式規範,定義日期和時間值的文字表示。A standard date and time format string uses a single format specifier to define the text representation of a date and time value. 任何包含一個以上字元 (包括空白字元) 的日期和時間格式字串都會解譯為自訂日期和時間格式字串;如需詳細資訊,請參閱自訂日期和時間格式字串Any date and time format string that contains more than one character, including white space, is interpreted as a custom date and time format string; for more information, see Custom Date and Time Format Strings. 標準或自訂格式字串有兩種使用方式:A standard or custom format string can be used in two ways:

  • 定義執行格式化作業後所產生的字串。To define the string that results from a formatting operation.

  • 定義日期和時間值的文字表示,可藉由剖析作業轉換成 DateTimeDateTimeOffset 值。To define the text representation of a date and time value that can be converted to a DateTime or DateTimeOffset value by a parsing operation.

提示

您可以下載格式化公用程式,這個 .NET Core Windows Forms 應用程式可讓您將格式字串套用至數值或日期和時間值,並顯示結果字串。You can download the Formatting Utility, a .NET Core Windows Forms application that lets you apply format strings to either numeric or date and time values and displays the result string. 提供 C#Visual Basic 的原始程式碼。Source code is available for C# and Visual Basic.

標準日期和時間格式字串可以與 DateTimeDateTimeOffset 值搭配使用。Standard date and time format strings can be used with both DateTime and DateTimeOffset values.

注意

本文中的某些 C# 範例會在 Try.NET 內嵌程式碼執行器和測試區執行。Some of the C# examples in this article run in the Try.NET inline code runner and playground. 選取 [執行] 按鈕以在互動式視窗中執行範例。Select the Run button to run an example in an interactive window. 執行程式碼之後,您便可以修改它,並再選取一次 [執行] 來執行修改過的程式碼。Once you execute the code, you can modify it and run the modified code by selecting Run again. 修改過的程式碼會在互動式視窗中執行,或是如果編譯失敗的話,互動式視窗會顯示所有 C# 編譯器錯誤訊息。The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages.

Try.NET 內嵌程式碼執行器與 Playground 的當地時區是全球標準時間 (UTC)。The local time zone of the Try.NET inline code runner and playground is Coordinated Universal Time, or UTC. 這可能會影響示範 DateTimeDateTimeOffsetTimeZoneInfo 型別與其成員之範例的行為與輸出。This may affect the behavior and the output of examples that illustrate the DateTime, DateTimeOffset, and TimeZoneInfo types and their members.

下表描述標準日期和時間的格式規範。The following table describes the standard date and time format specifiers. 除非特別註明,否則特定標準日期和時間格式規範會產生相同的字串表示,無論是與 DateTimeDateTimeOffset 值搭配使用。Unless otherwise noted, a particular standard date and time format specifier produces an identical string representation regardless of whether it is used with a DateTime or a DateTimeOffset value. 如需使用標準日期和時間格式字串的詳細資訊,請參閱備註一節。See the Notes section for additional information about using standard date and time format strings.

格式規範Format specifier 說明Description 範例Examples
"d""d" 簡短日期模式。Short date pattern.

詳細資訊:簡短日期 ("d") 格式規範More information:The Short Date ("d") Format Specifier.
2009-06-15T13:45:30 -> 6/15/2009 (en-US)2009-06-15T13:45:30 -> 6/15/2009 (en-US)

2009-06-15T13:45:30 -> 15/06/2009 (fr-FR)2009-06-15T13:45:30 -> 15/06/2009 (fr-FR)

2009-06-15T13:45:30 -> 2009/06/15 (ja-JP)2009-06-15T13:45:30 -> 2009/06/15 (ja-JP)
"D""D" 完整日期模式。Long date pattern.

詳細資訊:完整日期 ("D") 格式規範More information:The Long Date ("D") Format Specifier.
2009-06-15T13:45:30 -> Monday, June 15, 2009 (en-US)2009-06-15T13:45:30 -> Monday, June 15, 2009 (en-US)

2009-06-15T13:45:30 -> 15 июня 2009 г2009-06-15T13:45:30 -> 15 июня 2009 г. (ru-RU)(ru-RU)

2009-06-15T13:45:30 -> Montag, 152009-06-15T13:45:30 -> Montag, 15. Juni 2009 (de-DE)Juni 2009 (de-DE)
"f""f" 完整日期/時間模式 (簡短時間)。Full date/time pattern (short time).

詳細資訊:完整日期簡短時間 ("f") 格式規範More information: The Full Date Short Time ("f") Format Specifier.
2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45 PM (en-US)2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45 PM (en-US)

2009-06-15T13:45:30 -> den 15 juni 2009 13:45 (sv-SE)2009-06-15T13:45:30 -> den 15 juni 2009 13:45 (sv-SE)

2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 1:45 μμ (el-GR)2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 1:45 μμ (el-GR)
"F""F" 完整日期/時間模式 (完整時間)。Full date/time pattern (long time).

詳細資訊:完整日期完整時間 ("F") 格式規範More information: The Full Date Long Time ("F") Format Specifier.
2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45:30 PM (en-US)2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45:30 PM (en-US)

2009-06-15T13:45:30 -> den 15 juni 2009 13:45:30 (sv-SE)2009-06-15T13:45:30 -> den 15 juni 2009 13:45:30 (sv-SE)

2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 1:45:30 μμ (el-GR)2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 1:45:30 μμ (el-GR)
"g""g" 一般日期/時間模式 (簡短時間)。General date/time pattern (short time).

詳細資訊:一般日期簡短時間 ("g") 格式規範More information: The General Date Short Time ("g") Format Specifier.
2009-06-15T13:45:30 -> 6/15/2009 1:45 PM (en-US)2009-06-15T13:45:30 -> 6/15/2009 1:45 PM (en-US)

2009-06-15T13:45:30 -> 15/06/2009 13:45 (es-ES)2009-06-15T13:45:30 -> 15/06/2009 13:45 (es-ES)

2009-06-15T13:45:30 -> 2009/6/15 13:45 (zh-CN)2009-06-15T13:45:30 -> 2009/6/15 13:45 (zh-CN)
"G""G" 一般日期/時間模式 (完整時間)。General date/time pattern (long time).

詳細資訊:一般日期完整時間 ("G") 格式規範More information: The General Date Long Time ("G") Format Specifier.
2009-06-15T13:45:30 -> 6/15/2009 1:45:30 PM (en-US)2009-06-15T13:45:30 -> 6/15/2009 1:45:30 PM (en-US)

2009-06-15T13:45:30 -> 15/06/2009 13:45:30 (es-ES)2009-06-15T13:45:30 -> 15/06/2009 13:45:30 (es-ES)

2009-06-15T13:45:30 -> 2009/6/15 13:45:30 (zh-CN)2009-06-15T13:45:30 -> 2009/6/15 13:45:30 (zh-CN)
"M"、"m""M", "m" 月/日模式。Month/day pattern.

詳細資訊:月 ("M"、"m") 格式規範More information: The Month ("M", "m") Format Specifier.
2009-06-15T13:45:30 -> June 15 (en-US)2009-06-15T13:45:30 -> June 15 (en-US)

2009-06-15T13:45:30 -> 152009-06-15T13:45:30 -> 15. juni (da-DK)juni (da-DK)

2009-06-15T13:45:30 -> 15 Juni (id-ID)2009-06-15T13:45:30 -> 15 Juni (id-ID)
"O"、"o""O", "o" 來回日期/時間模式。Round-trip date/time pattern.

詳細資訊:來回 ("O"、"o") 格式規範More information: The Round-trip ("O", "o") Format Specifier.
DateTime 值:DateTime values:

2009-06-15T13:45:30 (DateTimeKind.Local) --> 2009-06-15T13:45:30.0000000-07:002009-06-15T13:45:30 (DateTimeKind.Local) --> 2009-06-15T13:45:30.0000000-07:00

2009-06-15T13:45:30 (DateTimeKind.Utc) --> 2009-06-15T13:45:30.0000000Z2009-06-15T13:45:30 (DateTimeKind.Utc) --> 2009-06-15T13:45:30.0000000Z

2009-06-15T13:45:30 (DateTimeKind.Unspecified) --> 2009-06-15T13:45:30.00000002009-06-15T13:45:30 (DateTimeKind.Unspecified) --> 2009-06-15T13:45:30.0000000

DateTimeOffset 值:DateTimeOffset values:

2009-06-15T13:45:30-07:00 --> 2009-06-15T13:45:30.0000000-07:002009-06-15T13:45:30-07:00 --> 2009-06-15T13:45:30.0000000-07:00
"R"、"r""R", "r" RFC1123 模式。RFC1123 pattern.

詳細資訊:RFC1123 ("R"、"r") 格式規範More information: The RFC1123 ("R", "r") Format Specifier.
2009-06-15T13:45:30 -> Mon, 15 Jun 2009 20:45:30 GMT2009-06-15T13:45:30 -> Mon, 15 Jun 2009 20:45:30 GMT
"s""s" 可排序日期/時間模式。Sortable date/time pattern.

詳細資訊:可排序 ("s") 格式規範More information: The Sortable ("s") Format Specifier.
2009-06-15T13:45:30 (DateTimeKind.Local) -> 2009-06-15T13:45:302009-06-15T13:45:30 (DateTimeKind.Local) -> 2009-06-15T13:45:30

2009-06-15T13:45:30 (DateTimeKind.Utc) -> 2009-06-15T13:45:302009-06-15T13:45:30 (DateTimeKind.Utc) -> 2009-06-15T13:45:30
"t""t" 簡短時間模式。Short time pattern.

詳細資訊:簡短時間 ("t") 格式規範More information: The Short Time ("t") Format Specifier.
2009-06-15T13:45:30 -> 1:45 PM (en-US)2009-06-15T13:45:30 -> 1:45 PM (en-US)

2009-06-15T13:45:30 -> 13:45 (hr-HR)2009-06-15T13:45:30 -> 13:45 (hr-HR)

2009-06-15T13:45:30 -> 01:45 م (ar-EG)2009-06-15T13:45:30 -> 01:45 م (ar-EG)
"T""T" 完整時間模式。Long time pattern.

詳細資訊:完整時間 ("T") 格式規範More information: The Long Time ("T") Format Specifier.
2009-06-15T13:45:30 -> 1:45:30 PM (en-US)2009-06-15T13:45:30 -> 1:45:30 PM (en-US)

2009-06-15T13:45:30 -> 13:45:30 (hr-HR)2009-06-15T13:45:30 -> 13:45:30 (hr-HR)

2009-06-15T13:45:30 -> 01:45:30 م (ar-EG)2009-06-15T13:45:30 -> 01:45:30 م (ar-EG)
"u""u" 國際可排序日期/時間模式。Universal sortable date/time pattern.

詳細資訊:國際可排序 ("u") 格式規範More information: The Universal Sortable ("u") Format Specifier.
具有 DateTime 值:2009-06-15T13:45:30 -> 2009-06-15 13:45:30ZWith a DateTime value: 2009-06-15T13:45:30 -> 2009-06-15 13:45:30Z

具有 DateTimeOffset 值:2009-06-15T13:45:30 -> 2009-06-15 20:45:30ZWith a DateTimeOffset value: 2009-06-15T13:45:30 -> 2009-06-15 20:45:30Z
"U""U" 國際完整日期/時間模式。Universal full date/time pattern.

詳細資訊:國際完整 ("U") 格式規範More information: The Universal Full ("U") Format Specifier.
2009-06-15T13:45:30 -> Monday, June 15, 2009 8:45:30 PM (en-US)2009-06-15T13:45:30 -> Monday, June 15, 2009 8:45:30 PM (en-US)

2009-06-15T13:45:30 -> den 15 juni 2009 20:45:30 (sv-SE)2009-06-15T13:45:30 -> den 15 juni 2009 20:45:30 (sv-SE)

2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 8:45:30 μμ (el-GR)2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 8:45:30 μμ (el-GR)
"Y", "y""Y", "y" 年月模式。Year month pattern.

詳細資訊:年月 ("Y") 格式規範More information: The Year Month ("Y") Format Specifier.
2009-06-15T13:45:30 -> June, 2009 (en-US)2009-06-15T13:45:30 -> June, 2009 (en-US)

2009-06-15T13:45:30 -> juni 2009 (da-DK)2009-06-15T13:45:30 -> juni 2009 (da-DK)

2009-06-15T13:45:30 -> Juni 2009 (id-ID)2009-06-15T13:45:30 -> Juni 2009 (id-ID)
任何其他單一字元Any other single character 未知的規範。Unknown specifier. 擲回執行階段 FormatExceptionThrows a run-time FormatException.

標準格式字串的運作方式How Standard Format Strings Work

在格式化作業中,標準格式字串只是自訂格式字串的別名。In a formatting operation, a standard format string is simply an alias for a custom format string. 使用別名來表示自訂格式字串的好處是,儘管別名保持不變,自訂格式字串本身則可有變化。The advantage of using an alias to refer to a custom format string is that, although the alias remains invariant, the custom format string itself can vary. 這點非常重要,因為日期和時間值的字串表示通常會因文化特性而不同。This is important because the string representations of date and time values typically vary by culture. 例如,"d" 標準格式字串表示日期和時間值將使用簡短日期模式顯示。For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. 在不因文化特性而異 (Invariant Culture) 的情況下,此模式為 "MM/dd/yyyy"。For the invariant culture, this pattern is "MM/dd/yyyy". 若為 fr-FR 文化特性,則是 "dd/MM/yyyy"。For the fr-FR culture, it is "dd/MM/yyyy". 若為 ja-JP 文化特性,則是 "yyyy/MM/dd"。For the ja-JP culture, it is "yyyy/MM/dd".

如果格式化作業中的標準格式字串對應至特定文化特性的自訂格式字串,您的應用程式可以定義特定文化特性,以下列其中一種方式使用自訂格式字串:If a standard format string in a formatting operation maps to a particular culture's custom format string, your application can define the specific culture whose custom format strings are used in one of these ways:

  • 您可以使用預設 (或目前的) 文化特性。You can use the default (or current) culture. 下列範例顯示的日期使用了目前文化特性的簡短日期格式。The following example displays a date using the current culture's short date format. 在此例中,目前的文化特性是 en-US。In this case, the current culture is en-US.

    // 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
    
    ' 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
    
  • 您可以將表示要用來進行格式化之文化特性的 CultureInfo 物件傳遞至具有 IFormatProvider 參數的方法。You can pass a CultureInfo object representing the culture whose formatting is to be used to a method that has an IFormatProvider parameter. 下列範例顯示的日期使用了 pt-BR 文化特性的簡短日期格式。The following example displays a date using the short date format of the pt-BR culture.

    // 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
    
    ' 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
    
  • 您可以將提供格式化資訊的 DateTimeFormatInfo 物件傳遞至具有 IFormatProvider 參數的方法。You can pass a DateTimeFormatInfo object that provides formatting information to a method that has an IFormatProvider parameter. 下列範例顯示的日期使用了取自於 hr-HR 文化特性之 DateTimeFormatInfo 物件的簡短日期格式。The following example displays a date using the short date format from a DateTimeFormatInfo object for the hr-HR culture.

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

注意

如需有關自訂格式化日期與時間值所使用之模式或字串的詳細資訊,請參閱 NumberFormatInfo 類別主題。For information about customizing the patterns or strings used in formatting date and time values, see the NumberFormatInfo class topic.

在某些情況下,標準格式字串可做為不變之長自訂格式字串的簡便縮寫。In some cases, the standard format string serves as a convenient abbreviation for a longer custom format string that is invariant. 有四個標準格式字串屬於此分類:"O" (或 "o")、"R" (或 "r")、"s" 和 "u"。Four standard format strings fall into this category: "O" (or "o"), "R" (or "r"), "s", and "u". 這些字串相當於不因文化特性而異所定義的自訂格式字串。These strings correspond to custom format strings defined by the invariant culture. 它們針對日期和時間值所產生的字串表示在各文化特性中都相同。They produce string representations of date and time values that are intended to be identical across cultures. 下表提供這四種標準日期和時間格式字串的相關資訊。The following table provides information on these four standard date and time format strings.

標準格式字串Standard format string 由 DateTimeFormatInfo.InvariantInfo 屬性定義Defined by DateTimeFormatInfo.InvariantInfo property 自訂格式字串Custom format string
"O" 或 "o""O" or "o" NoneNone yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzyyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzz
"R" 或 "r""R" or "r" RFC1123Pattern ddd、dd MMM yyyy HH':'mm':'ss 'GMT'ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
"s""s" SortableDateTimePattern yyyy'-'MM'-'dd'T'HH':'mm':'ssyyyy'-'MM'-'dd'T'HH':'mm':'ss
"u""u" UniversalSortableDateTimePattern yyyy'-'MM'-'dd HH':'mm':'ss'Z'yyyy'-'MM'-'dd HH':'mm':'ss'Z'

標準格式字串也可以在剖析作業中搭配 DateTime.ParseExactDateTimeOffset.ParseExact 方法使用,這些方法都需要輸入字串完全符合特定模式,剖析作業才會成功。Standard format strings can also be used in parsing operations with the DateTime.ParseExact or DateTimeOffset.ParseExact methods, which require an input string to exactly conform to a particular pattern for the parse operation to succeed. 許多標準格式字串都對應至多個自訂格式字串,因此日期和時間值可以用各種不同的格式表示,而剖析作業仍然會成功。Many standard format strings map to multiple custom format strings, so a date and time value can be represented in a variety of formats and the parse operation will still succeed. 您可以藉由呼叫 DateTimeFormatInfo.GetAllDateTimePatterns(Char) 方法判斷對應至標準格式字串的自訂格式字串。You can determine the custom format string or strings that correspond to a standard format string by calling the DateTimeFormatInfo.GetAllDateTimePatterns(Char) method. 下列範例會顯示對應至 "d" (簡短日期模式) 標準格式字串的自訂格式字串。The following example displays the custom format strings that map to the "d" (short date pattern) standard format string.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("'d' standard format string:");
      foreach (var customString in DateTimeFormatInfo.CurrentInfo.GetAllDateTimePatterns('d'))
          Console.WriteLine("   {0}", customString);
   }
}
// The example displays the following output:
//       'd' standard format string:
//          M/d/yyyy
//          M/d/yy
//          MM/dd/yy
//          MM/dd/yyyy
//          yy/MM/dd
//          yyyy-MM-dd
//          dd-MMM-yy
Imports System.Globalization

Module Example
   Public Sub Main()
      Console.WriteLine("'d' standard format string:")
      For Each customString In DateTimeFormatInfo.CurrentInfo.GetAllDateTimePatterns("d"c)
          Console.WriteLine("   {0}", customString)
      Next                                  
   End Sub
End Module
' The example displays the following output:
'    'd' standard format string:
'       M/d/yyyy
'       M/d/yy
'       MM/dd/yy
'       MM/dd/yyyy
'       yy/MM/dd
'       yyyy-MM-dd
'       dd-MMM-yy

下列各節描述 DateTimeDateTimeOffset 值的標準格式規範。The following sections describe the standard format specifiers for DateTime and DateTimeOffset values.

簡短日期 ("d") 格式規範The Short Date ("d") Format Specifier

"d" 標準格式規範表示由特定文化特性之 DateTimeFormatInfo.ShortDatePattern 屬性所定義的自訂日期和時間格式字串。The "d" standard format specifier represents a custom date and time format string that is defined by a specific culture's DateTimeFormatInfo.ShortDatePattern property. 例如,不因文化特性而異的 ShortDatePattern 屬性傳回的自訂格式字串為 "MM/dd/yyyy"。For example, the custom format string that is returned by the ShortDatePattern property of the invariant culture is "MM/dd/yyyy".

下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that control the formatting of the returned string.

屬性Property 說明Description
ShortDatePattern 定義結果字串的整體格式。Defines the overall format of the result string.
DateSeparator 定義分隔日期之年份、月份和日期元件的字串。Defines the string that separates the year, month, and day components of a date.

下列範例使用 "d" 格式規範來顯示日期和時間值。The following example uses the "d" format specifier to display a date and time value.

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

回到表格Back to table

完整日期 ("D") 格式規範The Long Date ("D") Format Specifier

"D" 標準格式規範表示由目前 DateTimeFormatInfo.LongDatePattern 屬性所定義的自訂日期和時間格式字串。The "D" standard format specifier represents a custom date and time format string that is defined by the current DateTimeFormatInfo.LongDatePattern property. 例如,不因文化特性而異的自訂格式字串為 "dddd, dd MMMM yyyy"。For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy".

下表列出 DateTimeFormatInfo 物件的屬性,這些屬性控制傳回之字串的格式設定。The following table lists the properties of the DateTimeFormatInfo object that control the formatting of the returned string.

屬性Property 說明Description
LongDatePattern 定義結果字串的整體格式。Defines the overall format of the result string.
DayNames 定義可顯示在結果字串中的當地語系化日期名稱。Defines the localized day names that can appear in the result string.
MonthNames 定義可顯示在結果字串中的當地語系化月份名稱。Defines the localized month names that can appear in the result string.

下列範例使用 "D" 格式規範來顯示日期和時間值。The following example uses the "D" format specifier to display a date and time value.

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

回到表格Back to table

完整日期簡短時間 ("f") 格式規範The Full Date Short Time ("f") Format Specifier

"f" 標準格式規範表示完整日期 ("D") 和簡短時間 ("t") 模式的組合,以空格分隔。The "f" standard format specifier represents a combination of the long date ("D") and short time ("t") patterns, separated by a space.

結果字串會受到特定 DateTimeFormatInfo 物件的格式設定資訊所影響。The result string is affected by the formatting information of a specific DateTimeFormatInfo object. 下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that may control the formatting of the returned string. 由某些文化特性的 DateTimeFormatInfo.LongDatePatternDateTimeFormatInfo.ShortTimePattern 屬性所傳回的自訂格式規範,可能不會使用所有屬性。The custom format specifier returned by the DateTimeFormatInfo.LongDatePattern and DateTimeFormatInfo.ShortTimePattern properties of some cultures may not make use of all properties.

屬性Property 說明Description
LongDatePattern 定義結果字串之日期元件的格式。Defines the format of the date component of the result string.
ShortTimePattern 定義結果字串之時間元件的格式。Defines the format of the time component of the result string.
DayNames 定義可顯示在結果字串中的當地語系化日期名稱。Defines the localized day names that can appear in the result string.
MonthNames 定義可顯示在結果字串中的當地語系化月份名稱。Defines the localized month names that can appear in the result string.
TimeSeparator 定義分隔時間之小時、分鐘和秒鐘元件的字串。Defines the string that separates the hour, minute, and second components of a time.
AMDesignator 定義表示從午夜到中午之前時間 (12 小時制) 的字串。Defines the string that indicates times from midnight to before noon in a 12-hour clock.
PMDesignator 定義表示從中午到午夜之前時間 (12 小時制) 的字串。Defines the string that indicates times from noon to before midnight in a 12-hour clock.

下列範例使用 "f" 格式規範來顯示日期和時間值。The following example uses the "f" format specifier to display a date and time value.

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

回到表格Back to table

完整日期完整時間 ("F") 格式規範The Full Date Long Time ("F") Format Specifier

"F" 標準格式規範表示由目前 DateTimeFormatInfo.FullDateTimePattern 屬性所定義的自訂日期和時間格式字串。The "F" standard format specifier represents a custom date and time format string that is defined by the current DateTimeFormatInfo.FullDateTimePattern property. 例如,不因文化特性而異的自訂格式字串為 "dddd, dd MMMM yyyy HH:mm:ss"。For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy HH:mm:ss".

下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that may control the formatting of the returned string. 由某些文化特性的 FullDateTimePattern 屬性所傳回的自訂格式規範,可能不會使用所有屬性。The custom format specifier that is returned by the FullDateTimePattern property of some cultures may not make use of all properties.

屬性Property 說明Description
FullDateTimePattern 定義結果字串的整體格式。Defines the overall format of the result string.
DayNames 定義可顯示在結果字串中的當地語系化日期名稱。Defines the localized day names that can appear in the result string.
MonthNames 定義可顯示在結果字串中的當地語系化月份名稱。Defines the localized month names that can appear in the result string.
TimeSeparator 定義分隔時間之小時、分鐘和秒鐘元件的字串。Defines the string that separates the hour, minute, and second components of a time.
AMDesignator 定義表示從午夜到中午之前時間 (12 小時制) 的字串。Defines the string that indicates times from midnight to before noon in a 12-hour clock.
PMDesignator 定義表示從中午到午夜之前時間 (12 小時制) 的字串。Defines the string that indicates times from noon to before midnight in a 12-hour clock.

下列範例使用 "F" 格式規範來顯示日期和時間值。The following example uses the "F" format specifier to display a date and time value.

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

回到表格Back to table

一般日期簡短時間 ("g") 格式規範The General Date Short Time ("g") Format Specifier

"g" 標準格式規範表示簡短日期 ("d") 和簡短時間 ("t") 模式的組合,以空格分隔。The "g" standard format specifier represents a combination of the short date ("d") and short time ("t") patterns, separated by a space.

結果字串會受到特定 DateTimeFormatInfo 物件的格式設定資訊所影響。The result string is affected by the formatting information of a specific DateTimeFormatInfo object. 下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that may control the formatting of the returned string. 由某些文化特性的 DateTimeFormatInfo.ShortDatePatternDateTimeFormatInfo.ShortTimePattern 屬性所傳回的自訂格式規範,可能不會使用所有屬性。The custom format specifier that is returned by the DateTimeFormatInfo.ShortDatePattern and DateTimeFormatInfo.ShortTimePattern properties of some cultures may not make use of all properties.

屬性Property 說明Description
ShortDatePattern 定義結果字串之日期元件的格式。Defines the format of the date component of the result string.
ShortTimePattern 定義結果字串之時間元件的格式。Defines the format of the time component of the result string.
DateSeparator 定義分隔日期之年份、月份和日期元件的字串。Defines the string that separates the year, month, and day components of a date.
TimeSeparator 定義分隔時間之小時、分鐘和秒鐘元件的字串。Defines the string that separates the hour, minute, and second components of a time.
AMDesignator 定義表示從午夜到中午之前時間 (12 小時制) 的字串。Defines the string that indicates times from midnight to before noon in a 12-hour clock.
PMDesignator 定義表示從中午到午夜之前時間 (12 小時制) 的字串。Defines the string that indicates times from noon to before midnight in a 12-hour clock.

下列範例使用 "g" 格式規範來顯示日期和時間值。The following example uses the "g" format specifier to display a date and time value.

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")));
// Displays 10/04/2008 6:30                        
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")))
' Displays 10/04/2008 6:30                        

回到表格Back to table

一般日期完整時間 ("G") 格式規範The General Date Long Time ("G") Format Specifier

"G" 標準格式規範表示簡短日期 ("d") 和完整時間 ("T") 模式的組合,以空格分隔。The "G" standard format specifier represents a combination of the short date ("d") and long time ("T") patterns, separated by a space.

結果字串會受到特定 DateTimeFormatInfo 物件的格式設定資訊所影響。The result string is affected by the formatting information of a specific DateTimeFormatInfo object. 下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that may control the formatting of the returned string. 由某些文化特性的 DateTimeFormatInfo.ShortDatePatternDateTimeFormatInfo.LongTimePattern 屬性所傳回的自訂格式規範,可能不會使用所有屬性。The custom format specifier that is returned by the DateTimeFormatInfo.ShortDatePattern and DateTimeFormatInfo.LongTimePattern properties of some cultures may not make use of all properties.

屬性Property 說明Description
ShortDatePattern 定義結果字串之日期元件的格式。Defines the format of the date component of the result string.
LongTimePattern 定義結果字串之時間元件的格式。Defines the format of the time component of the result string.
DateSeparator 定義分隔日期之年份、月份和日期元件的字串。Defines the string that separates the year, month, and day components of a date.
TimeSeparator 定義分隔時間之小時、分鐘和秒鐘元件的字串。Defines the string that separates the hour, minute, and second components of a time.
AMDesignator 定義表示從午夜到中午之前時間 (12 小時制) 的字串。Defines the string that indicates times from midnight to before noon in a 12-hour clock.
PMDesignator 定義表示從中午到午夜之前時間 (12 小時制) 的字串。Defines the string that indicates times from noon to before midnight in a 12-hour clock.

下列範例使用 "G" 格式規範來顯示日期和時間值。The following example uses the "G" format specifier to display a date and time value.

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

回到表格Back to table

月 ("M"、"m") 格式規範The Month ("M", "m") Format Specifier

"M" 或 "m" 標準格式規範表示由目前 DateTimeFormatInfo.MonthDayPattern 屬性所定義的自訂日期和時間格式字串。The "M" or "m" standard format specifier represents a custom date and time format string that is defined by the current DateTimeFormatInfo.MonthDayPattern property. 例如,不因文化特性而異的自訂格式字串為 "MMMM dd"。For example, the custom format string for the invariant culture is "MMMM dd".

下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that control the formatting of the returned string.

屬性Property 說明Description
MonthDayPattern 定義結果字串的整體格式。Defines the overall format of the result string.
MonthNames 定義可顯示在結果字串中的當地語系化月份名稱。Defines the localized month names that can appear in the result string.

下列範例使用 "m" 格式規範來顯示日期和時間值。The following example uses the "m" format specifier to display a date and time value.

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

回到表格Back to table

來回 ("O"、"o") 格式規範The Round-trip ("O", "o") Format Specifier

"O" 或 "o" 標準格式規範可表示使用保存時區資訊之模式的自訂日期和時間格式字串,並發出符合 ISO 8601 的結果字串。The "O" or "o" standard format specifier represents a custom date and time format string using a pattern that preserves time zone information and emits a result string that complies with ISO 8601. 若為 DateTime 值,此格式規範是設計來以純文字保存日期和時間值以及 DateTime.Kind 屬性。For DateTime values, this format specifier is designed to preserve date and time values along with the DateTime.Kind property in text. 如果將 DateTime.Parse(String, IFormatProvider, DateTimeStyles)DateTime.ParseExact 方法的 styles 參數設定為 DateTimeStyles.RoundtripKind,則可以使用這兩個方法來剖析還原格式化字串。The formatted string can be parsed back by using the DateTime.Parse(String, IFormatProvider, DateTimeStyles) or DateTime.ParseExact method if the styles parameter is set to DateTimeStyles.RoundtripKind.

"O" 或 "o" 標準格式規範對應至 DateTime 值的 "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" 自訂格式字串,也對應至 DateTimeOffset 值的 "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz" 自訂格式字串。The "O" or "o" standard format specifier corresponds to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string for DateTime values and to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz" custom format string for DateTimeOffset values. 在此字串中,分隔個別字元 (例如連字號、冒號和字母 "T") 的各組單引號表示個別字元為不可變更的常值。In this string, the pairs of single quotation marks that delimit individual characters, such as the hyphens, the colons, and the letter "T", indicate that the individual character is a literal that cannot be changed. 所有格符號不會出現在輸出字串中。The apostrophes do not appear in the output string.

"O" 或 "o" 標準格式規範 (以及 "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" 自訂格式字串) 利用 ISO 8601 表示時區資訊的三種方式,來保留 DateTime 值的 Kind 屬性:The "O" or "o" standard format specifier (and the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string) takes advantage of the three ways that ISO 8601 represents time zone information to preserve the Kind property of DateTime values:

因為 "O" 或 "o" 標準格式規範符合國際標準,所以使用該規範的格式化或剖析作業一律使用不因國別而異的文化特性和西曆。Because the "O" or "o" standard format specifier conforms to an international standard, the formatting or parsing operation that uses the specifier always uses the invariant culture and the Gregorian calendar.

傳遞至 ParseTryParseParseExactTryParseExactDateTimeDateTimeOffset 方法的字串如果是這些格式,就可以使用 "O" 或 "o" 格式規範來剖析。Strings that are passed to the Parse, TryParse, ParseExact, and TryParseExact methods of DateTime and DateTimeOffset can be parsed by using the "O" or "o" format specifier if they are in one of these formats. DateTime 物件的案例中,您呼叫的剖析多載應該也要包含 styles 參數,且值為 DateTimeStyles.RoundtripKindIn the case of DateTime objects, the parsing overload that you call should also include a styles parameter with a value of DateTimeStyles.RoundtripKind. 請注意,如果您呼叫剖析方法時,使用對應於 "O" 或 "o" 格式規範的自訂格式字串,將不會取得與 "O" 或 "o" 相同的結果。Note that if you call a parsing method with the custom format string that corresponds to the "O" or "o" format specifier, you won't get the same results as "O" or "o". 這是因為使用自訂格式字串的剖析方法,無法剖析缺少時區元件或使用 "Z" 來指示 UTC 之日期和時間值的字串表示法。This is because parsing methods that use a custom format string can't parse the string representation of date and time values that lack a time zone component or use "Z" to indicate UTC.

下列範例使用 "o" 格式規範,在美國太平洋時區系統上顯示一連串的 DateTime 值以及 DateTimeOffset 值太平洋時間The following example uses the "o" format specifier to display a series of DateTime values and a DateTimeOffset value on a system in the U.S. Pacific Time zone.

using System;

public class Example
{
   public static void Main()
   {
       DateTime dat = new DateTime(2009, 6, 15, 13, 45, 30, 
                                   DateTimeKind.Unspecified);
       Console.WriteLine("{0} ({1}) --> {0:O}", dat, dat.Kind); 
   
       DateTime uDat = new DateTime(2009, 6, 15, 13, 45, 30, 
                                    DateTimeKind.Utc);
       Console.WriteLine("{0} ({1}) --> {0:O}", uDat, uDat.Kind);
       
       DateTime lDat = new DateTime(2009, 6, 15, 13, 45, 30, 
                                    DateTimeKind.Local);
       Console.WriteLine("{0} ({1}) --> {0:O}\n", lDat, lDat.Kind);
       
       DateTimeOffset dto = new DateTimeOffset(lDat);
       Console.WriteLine("{0} --> {0:O}", dto);
   }
}
// The example displays the following output:
//    6/15/2009 1:45:30 PM (Unspecified) --> 2009-06-15T13:45:30.0000000
//    6/15/2009 1:45:30 PM (Utc) --> 2009-06-15T13:45:30.0000000Z
//    6/15/2009 1:45:30 PM (Local) --> 2009-06-15T13:45:30.0000000-07:00
//    
//    6/15/2009 1:45:30 PM -07:00 --> 2009-06-15T13:45:30.0000000-07:00
Module Example
   Public Sub Main()
       Dim dat As New Date(2009, 6, 15, 13, 45, 30, 
                           DateTimeKind.Unspecified)
       Console.WriteLine("{0} ({1}) --> {0:O}", dat, dat.Kind) 
   
       Dim uDat As New Date(2009, 6, 15, 13, 45, 30, DateTimeKind.Utc)
       Console.WriteLine("{0} ({1}) --> {0:O}", uDat, uDat.Kind)
       
       Dim lDat As New Date(2009, 6, 15, 13, 45, 30, DateTimeKind.Local)
       Console.WriteLine("{0} ({1}) --> {0:O}", lDat, lDat.Kind)
       Console.WriteLine()
       
       Dim dto As New DateTimeOffset(lDat)
       Console.WriteLine("{0} --> {0:O}", dto)
   End Sub
End Module
' The example displays the following output:
'    6/15/2009 1:45:30 PM (Unspecified) --> 2009-06-15T13:45:30.0000000
'    6/15/2009 1:45:30 PM (Utc) --> 2009-06-15T13:45:30.0000000Z
'    6/15/2009 1:45:30 PM (Local) --> 2009-06-15T13:45:30.0000000-07:00
'    
'    6/15/2009 1:45:30 PM -07:00 --> 2009-06-15T13:45:30.0000000-07:00

下列範例使用 "o" 格式規範建立格式化字串,然後呼叫日期和時間 Parse 方法來還原原始日期和時間值。The following example uses the "o" format specifier to create a formatted string, and then restores the original date and time value by calling a date and time Parse method.

// Round-trip DateTime values.
DateTime originalDate, newDate;
string dateString;
// Round-trip a local time.
originalDate = DateTime.SpecifyKind(new DateTime(2008, 4, 10, 6, 30, 0), DateTimeKind.Local);
dateString = originalDate.ToString("o");
newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);
Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind, 
                  newDate, newDate.Kind);
// Round-trip a UTC time.
originalDate = DateTime.SpecifyKind(new DateTime(2008, 4, 12, 9, 30, 0), DateTimeKind.Utc);                  
dateString = originalDate.ToString("o");
newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);
Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind, 
                  newDate, newDate.Kind);
// Round-trip time in an unspecified time zone.
originalDate = DateTime.SpecifyKind(new DateTime(2008, 4, 13, 12, 30, 0), DateTimeKind.Unspecified);                  
dateString = originalDate.ToString("o");
newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);
Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind, 
                  newDate, newDate.Kind);

// Round-trip a DateTimeOffset value.
DateTimeOffset originalDTO = new DateTimeOffset(2008, 4, 12, 9, 30, 0, new TimeSpan(-8, 0, 0));
dateString = originalDTO.ToString("o");
DateTimeOffset newDTO = DateTimeOffset.Parse(dateString, null, DateTimeStyles.RoundtripKind);
Console.WriteLine("Round-tripped {0} to {1}.", originalDTO, newDTO);
// The example displays the following output:
//    Round-tripped 4/10/2008 6:30:00 AM Local to 4/10/2008 6:30:00 AM Local.
//    Round-tripped 4/12/2008 9:30:00 AM Utc to 4/12/2008 9:30:00 AM Utc.
//    Round-tripped 4/13/2008 12:30:00 PM Unspecified to 4/13/2008 12:30:00 PM Unspecified.
//    Round-tripped 4/12/2008 9:30:00 AM -08:00 to 4/12/2008 9:30:00 AM -08:00.
' Round-trip DateTime values.
Dim originalDate, newDate As Date
Dim dateString As String
' Round-trip a local time.
originalDate = Date.SpecifyKind(#4/10/2008 6:30AM#, DateTimeKind.Local)
dateString = originalDate.ToString("o")
newDate = Date.Parse(dateString, Nothing, DateTimeStyles.RoundtripKind)
Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind, _
                  newDate, newDate.Kind)
' Round-trip a UTC time.
originalDate = Date.SpecifyKind(#4/12/2008 9:30AM#, DateTimeKind.Utc)                  
dateString = originalDate.ToString("o")
newDate = Date.Parse(dateString, Nothing, DateTimeStyles.RoundtripKind)
Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind, _
                  newDate, newDate.Kind)
' Round-trip time in an unspecified time zone.
originalDate = Date.SpecifyKind(#4/13/2008 12:30PM#, DateTimeKind.Unspecified)                  
dateString = originalDate.ToString("o")
newDate = Date.Parse(dateString, Nothing, DateTimeStyles.RoundtripKind)
Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", originalDate, originalDate.Kind, _
                  newDate, newDate.Kind)

' Round-trip a DateTimeOffset value.
Dim originalDTO As New DateTimeOffset(#4/12/2008 9:30AM#, New TimeSpan(-8, 0, 0))
dateString = originalDTO.ToString("o")
Dim newDTO As DateTimeOffset = DateTimeOffset.Parse(dateString, Nothing, DateTimeStyles.RoundtripKind)
Console.WriteLine("Round-tripped {0} to {1}.", originalDTO, newDTO)
' The example displays the following output:
'    Round-tripped 4/10/2008 6:30:00 AM Local to 4/10/2008 6:30:00 AM Local.
'    Round-tripped 4/12/2008 9:30:00 AM Utc to 4/12/2008 9:30:00 AM Utc.
'    Round-tripped 4/13/2008 12:30:00 PM Unspecified to 4/13/2008 12:30:00 PM Unspecified.
'    Round-tripped 4/12/2008 9:30:00 AM -08:00 to 4/12/2008 9:30:00 AM -08:00.

回到表格Back to table

RFC1123 ("R"、"r") 格式規範The RFC1123 ("R", "r") Format Specifier

"R" 或 "r" 標準格式規範表示由 DateTimeFormatInfo.RFC1123Pattern 屬性所定義的自訂日期和時間格式字串。The "R" or "r" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.RFC1123Pattern property. 此模式反映已定義的標準,且屬性為唯讀。The pattern reflects a defined standard, and the property is read-only. 因此,不論所使用的文化特性或提供的格式提供者為何,它一定會是相同的。Therefore, it is always the same, regardless of the culture used or the format provider supplied. 自訂格式字串為 "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"。The custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'". 使用這個標準格式規範時,格式或剖析作業一律使用不因文化特性而異。When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.

在表示不因文化特性而異之 DateTimeFormatInfo 屬性所傳回的 DateTimeFormatInfo.InvariantInfo 物件中,有下列屬性會影響結果字串。The result string is affected by the following properties of the DateTimeFormatInfo object returned by the DateTimeFormatInfo.InvariantInfo property that represents the invariant culture.

屬性Property 說明Description
RFC1123Pattern 定義結果字串的格式。Defines the format of the result string.
AbbreviatedDayNames 定義可顯示在結果字串中的縮寫日期名稱。Defines the abbreviated day names that can appear in the result string.
AbbreviatedMonthNames 定義可顯示在結果字串中的縮寫月份名稱。Defines the abbreviated month names that can appear in the result string.

雖然 RFC 1123 標準以國際標準時間 (UTC) 來表示時間,但對於要進行格式化的 DateTime 物件,格式化作業並不會修改這些物件的值。Although the RFC 1123 standard expresses a time as Coordinated Universal Time (UTC), the formatting operation does not modify the value of the DateTime object that is being formatted. 因此,您必須先呼叫 DateTime 方法將 DateTime.ToUniversalTime 值轉換成 UTC,才能執行格式化作業。Therefore, you must convert the DateTime value to UTC by calling the DateTime.ToUniversalTime method before you perform the formatting operation. 相反地,DateTimeOffset 值會自動執行這項轉換,因此在格式化作業之前,不需要呼叫 DateTimeOffset.ToUniversalTime 方法。In contrast, DateTimeOffset values perform this conversion automatically; there is no need to call the DateTimeOffset.ToUniversalTime method before the formatting operation.

下列範例會使用 "r" 格式規範,在美國太平洋時區系統上顯示 DateTimeDateTimeOffset 值。太平洋時間The following example uses the "r" format specifier to display a DateTime and a DateTimeOffset value on a system in the U.S. Pacific Time zone.

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

回到表格Back to table

可排序 ("s") 格式規範The Sortable ("s") Format Specifier

"s" 標準格式規範表示由 DateTimeFormatInfo.SortableDateTimePattern 屬性所定義的自訂日期和時間格式字串。The "s" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.SortableDateTimePattern property. 此模式反映已定義的標準 (ISO 8601),且屬性為唯讀。The pattern reflects a defined standard (ISO 8601), and the property is read-only. 因此,不論所使用的文化特性或提供的格式提供者為何,它一定會是相同的。Therefore, it is always the same, regardless of the culture used or the format provider supplied. 自訂格式字串為 "yyyy'-'MM'-'dd'T'HH':'mm':'ss"。The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss".

"s" 格式規範的目的在於產生結果字串時,能夠根據日期和時間值,一致地以遞增或遞減順序排序。The purpose of the "s" format specifier is to produce result strings that sort consistently in ascending or descending order based on date and time values. 如此一來,雖然 "s" 標準格式規範會以一致的格式來表示日期和時間值,但是格式化作業不會修改日期和時間物件的值 (為了反映其 DateTime.Kind 屬性或其 DateTimeOffset.Offset 值已格式化該值)。As a result, although the "s" standard format specifier represents a date and time value in a consistent format, the formatting operation does not modify the value of the date and time object that is being formatted to reflect its DateTime.Kind property or its DateTimeOffset.Offset value. 例如,格式化日期和時間值 2014-11-15T18:32:17+00:00 和 2014-11-15T18:32:17+08:00 所產生的結果字串相同。For example, the result strings produced by formatting the date and time values 2014-11-15T18:32:17+00:00 and 2014-11-15T18:32:17+08:00 are identical.

使用這個標準格式規範時,格式或剖析作業一律使用不因文化特性而異。When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.

下列範例會使用 "r" 格式規範,在美國太平洋時區系統上顯示 DateTimeDateTimeOffset 值。太平洋時間The following example uses the "s" format specifier to display a DateTime and a DateTimeOffset value on a system in the U.S. Pacific Time zone.

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

回到表格Back to table

簡短時間 ("t") 格式規範The Short Time ("t") Format Specifier

"t" 標準格式規範表示由目前 DateTimeFormatInfo.ShortTimePattern 屬性所定義的自訂日期和時間格式字串。The "t" standard format specifier represents a custom date and time format string that is defined by the current DateTimeFormatInfo.ShortTimePattern property. 例如,不因文化特性而異的自訂格式字串為 "HH:mm"。For example, the custom format string for the invariant culture is "HH:mm".

結果字串會受到特定 DateTimeFormatInfo 物件的格式設定資訊所影響。The result string is affected by the formatting information of a specific DateTimeFormatInfo object. 下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that may control the formatting of the returned string. 由某些文化特性的 DateTimeFormatInfo.ShortTimePattern 屬性所傳回的自訂格式規範,可能不會使用所有屬性。The custom format specifier that is returned by the DateTimeFormatInfo.ShortTimePattern property of some cultures may not make use of all properties.

屬性Property 說明Description
ShortTimePattern 定義結果字串之時間元件的格式。Defines the format of the time component of the result string.
TimeSeparator 定義分隔時間之小時、分鐘和秒鐘元件的字串。Defines the string that separates the hour, minute, and second components of a time.
AMDesignator 定義表示從午夜到中午之前時間 (12 小時制) 的字串。Defines the string that indicates times from midnight to before noon in a 12-hour clock.
PMDesignator 定義表示從中午到午夜之前時間 (12 小時制) 的字串。Defines the string that indicates times from noon to before midnight in a 12-hour clock.

下列範例使用 "t" 格式規範來顯示日期和時間值。The following example uses the "t" format specifier to display a date and time value.

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

回到表格Back to table

完整時間 ("T") 格式規範The Long Time ("T") Format Specifier

"T" 標準格式規範表示由特定文化特性之 DateTimeFormatInfo.LongTimePattern 屬性所定義的自訂日期和時間格式字串。The "T" standard format specifier represents a custom date and time format string that is defined by a specific culture's DateTimeFormatInfo.LongTimePattern property. 例如,不因文化特性而異的自訂格式字串為 "HH:mm:ss"。For example, the custom format string for the invariant culture is "HH:mm:ss".

下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that may control the formatting of the returned string. 由某些文化特性的 DateTimeFormatInfo.LongTimePattern 屬性所傳回的自訂格式規範,可能不會使用所有屬性。The custom format specifier that is returned by the DateTimeFormatInfo.LongTimePattern property of some cultures may not make use of all properties.

屬性Property 說明Description
LongTimePattern 定義結果字串之時間元件的格式。Defines the format of the time component of the result string.
TimeSeparator 定義分隔時間之小時、分鐘和秒鐘元件的字串。Defines the string that separates the hour, minute, and second components of a time.
AMDesignator 定義表示從午夜到中午之前時間 (12 小時制) 的字串。Defines the string that indicates times from midnight to before noon in a 12-hour clock.
PMDesignator 定義表示從中午到午夜之前時間 (12 小時制) 的字串。Defines the string that indicates times from noon to before midnight in a 12-hour clock.

下列範例使用 "T" 格式規範來顯示日期和時間值。The following example uses the "T" format specifier to display a date and time value.

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

回到表格Back to table

國際可排序 ("u") 格式規範The Universal Sortable ("u") Format Specifier

"u" 標準格式規範表示由 DateTimeFormatInfo.UniversalSortableDateTimePattern 屬性所定義的自訂日期和時間格式字串。The "u" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.UniversalSortableDateTimePattern property. 此模式反映已定義的標準,且屬性為唯讀。The pattern reflects a defined standard, and the property is read-only. 因此,不論所使用的文化特性或提供的格式提供者為何,它一定會是相同的。Therefore, it is always the same, regardless of the culture used or the format provider supplied. 自訂格式字串為 "yyyy'-'MM'-'dd HH':'mm':'ss'Z'"。The custom format string is "yyyy'-'MM'-'dd HH':'mm':'ss'Z'". 使用這個標準格式規範時,格式或剖析作業一律使用不因文化特性而異。When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.

雖然結果字串應該以國際標準時間 (UTC) 來表示時間,但在格式化作業期間,原始 DateTime 值不會執行任何轉換。Although the result string should express a time as Coordinated Universal Time (UTC), no conversion of the original DateTime value is performed during the formatting operation. 因此,您必須先呼叫 DateTime 方法將 DateTime.ToUniversalTime 值轉換成 UTC,才能將它格式化。Therefore, you must convert a DateTime value to UTC by calling the DateTime.ToUniversalTime method before formatting it. 相反地,DateTimeOffset 值會自動執行這項轉換,因此在格式化作業之前,不需要呼叫 DateTimeOffset.ToUniversalTime 方法。In contrast, DateTimeOffset values perform this conversion automatically; there is no need to call the DateTimeOffset.ToUniversalTime method before the formatting operation.

下列範例使用 "u" 格式規範來顯示日期和時間值。The following example uses the "u" format specifier to display a date and time value.

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

回到表格Back to table

國際完整 ("U") 格式規範The Universal Full ("U") Format Specifier

"U" 標準格式規範表示由指定文化特性之 DateTimeFormatInfo.FullDateTimePattern 屬性所定義的自訂日期和時間格式字串。The "U" standard format specifier represents a custom date and time format string that is defined by a specified culture's DateTimeFormatInfo.FullDateTimePattern property. 此模式與 "F" 模式相同。The pattern is the same as the "F" pattern. 不過,DateTime 值在格式化之前會自動轉換為 UTC。However, the DateTime value is automatically converted to UTC before it is formatted.

下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that may control the formatting of the returned string. 由某些文化特性的 FullDateTimePattern 屬性所傳回的自訂格式規範,可能不會使用所有屬性。The custom format specifier that is returned by the FullDateTimePattern property of some cultures may not make use of all properties.

屬性Property 說明Description
FullDateTimePattern 定義結果字串的整體格式。Defines the overall format of the result string.
DayNames 定義可顯示在結果字串中的當地語系化日期名稱。Defines the localized day names that can appear in the result string.
MonthNames 定義可顯示在結果字串中的當地語系化月份名稱。Defines the localized month names that can appear in the result string.
TimeSeparator 定義分隔時間之小時、分鐘和秒鐘元件的字串。Defines the string that separates the hour, minute, and second components of a time.
AMDesignator 定義表示從午夜到中午之前時間 (12 小時制) 的字串。Defines the string that indicates times from midnight to before noon in a 12-hour clock.
PMDesignator 定義表示從中午到午夜之前時間 (12 小時制) 的字串。Defines the string that indicates times from noon to before midnight in a 12-hour clock.

DateTimeOffset 類型不支援 "U" 格式規範,如果使用它來格式化 FormatException 值,則會擲回 DateTimeOffsetThe "U" format specifier is not supported by the DateTimeOffset type and throws a FormatException if it is used to format a DateTimeOffset value.

下列範例使用 "U" 格式規範來顯示日期和時間值。The following example uses the "U" format specifier to display a date and time value.

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

回到表格Back to table

年月 ("Y"、"y") 格式規範The Year Month ("Y", "y") Format Specifier

"Y" 或 "y" 標準格式規範表示由指定文化特性之 DateTimeFormatInfo.YearMonthPattern 屬性所定義的自訂日期和時間格式字串。The "Y" or "y" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.YearMonthPattern property of a specified culture. 例如,不因文化特性而異的自訂格式字串為 "yyyy MMMM"。For example, the custom format string for the invariant culture is "yyyy MMMM".

下表列出可控制傳回字串之格式設定的 DateTimeFormatInfo 物件屬性。The following table lists the DateTimeFormatInfo object properties that control the formatting of the returned string.

屬性Property 說明Description
YearMonthPattern 定義結果字串的整體格式。Defines the overall format of the result string.
MonthNames 定義可顯示在結果字串中的當地語系化月份名稱。Defines the localized month names that can appear in the result string.

下列範例使用 "y" 格式規範來顯示日期和時間值。The following example uses the "y" format specifier to display a date and time value.

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

回到表格Back to table

注意Notes

控制台設定值Control Panel Settings

[控制台] 中 [ 地區及語言選項] 項目的設定會影響格式化作業所產生的結果字串。The settings in the Regional and Language Options item in Control Panel influence the result string produced by a formatting operation. 這些設定是用來初始化與目前執行緒文化特性相關的 DateTimeFormatInfo 物件,該物件會提供用來管理格式的值。These settings are used to initialize the DateTimeFormatInfo object associated with the current thread culture, which provides values used to govern formatting. 使用不同設定的電腦會產生不同的結果字串。Computers that use different settings generate different result strings.

此外,如果您使用 CultureInfo.CultureInfo(String) 建構函式來具現化新的 CultureInfo 物件,而此物件代表的文化特性與目前系統文化特性相同,則 [控制台] 中的 [ 地區及語言選項 ] 項目所建立的任何自訂都會套用至新的 CultureInfo 物件。In addition, if you use the CultureInfo.CultureInfo(String) constructor to instantiate a new CultureInfo object that represents the same culture as the current system culture, any customizations established by the Regional and Language Options item in Control Panel will be applied to the new CultureInfo object. 您可以使用 CultureInfo.CultureInfo(String, Boolean) 建構函式來建立不反映系統自訂的 CultureInfo 物件。You can use the CultureInfo.CultureInfo(String, Boolean) constructor to create a CultureInfo object that does not reflect a system's customizations.

DateTimeFormatInfo 屬性DateTimeFormatInfo Properties

格式會受到目前 DateTimeFormatInfo 物件的影響,而此物件是由目前執行緒文化特性隱含提供或由叫用格式之方法的 IFormatProvider 參數明確提供。Formatting is influenced by properties of the current DateTimeFormatInfo object, which is provided implicitly by the current thread culture or explicitly by the IFormatProvider parameter of the method that invokes formatting. 針對 IFormatProvider 參數,您的應用程式應指定代表文化特性的 CultureInfo 物件,或是指定代表特定文化特性之日期和時間格式化慣例的 DateTimeFormatInfo 物件。For the IFormatProvider parameter, your application should specify a CultureInfo object, which represents a culture, or a DateTimeFormatInfo object, which represents a particular culture's date and time formatting conventions. 許多標準日期和時間格式規範都是格式化模式的別名,這些模式是由目前 DateTimeFormatInfo 物件的屬性所定義。Many of the standard date and time format specifiers are aliases for formatting patterns defined by properties of the current DateTimeFormatInfo object. 您的應用程式可以變更對應 DateTimeFormatInfo 屬性的對應日期和時間格式模式,藉此改變某些標準日期和時間格式規範所產生的結果。Your application can change the result produced by some standard date and time format specifiers by changing the corresponding date and time format patterns of the corresponding DateTimeFormatInfo property.

另請參閱See also