自訂日期和時間格式字串Custom Date and Time Format Strings

日期和時間格式字串會定義對 DateTimeDateTimeOffset 值執行格式化作業後所產生的文字表示。A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation . 另外還會定義剖析作業所需日期和時間值的表示,以便成功地將字串轉換成日期和時間。It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time. 自訂格式字串是由一個或多個自訂日期和時間格式規範所組成。A custom format string consists of one or more custom date and time format specifiers. 任何不是標準日期和時間格式字串的字串都會被解譯為自訂日期和時間格式字串。Any string that is not a standard date and time format string is interpreted as a custom date and time format string.

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

提示

您可以下載格式化公用程式,這個應用程式可讓您將格式字串套用至日期和時間值或數值,並且顯示結果字串。You can download the Formatting Utility, an application that enables you to apply format strings to either date and time or numeric values and displays the result string.

在格式化作業中,自訂日期和時間格式字串可以搭配日期和時間執行個體的 ToString 方法或是支援複合格式的方法使用。In formatting operations, custom date and time format strings can be used either with the ToString method of a date and time instance or with a method that supports composite formatting. 以下範例說明這兩種用法。The following example illustrates both uses.

DateTime thisDate1 = new DateTime(2011, 6, 10);
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".");
 
DateTimeOffset thisDate2 = new DateTimeOffset(2011, 6, 10, 15, 24, 16, 
                                              TimeSpan.Zero);
Console.WriteLine("The current date and time: {0:MM/dd/yy H:mm:ss zzz}", 
                   thisDate2); 
// The example displays the following output:
//    Today is June 10, 2011.
//    The current date and time: 06/10/11 15:24:16 +00:00
 Dim thisDate1 As Date = #6/10/2011#
 Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".")
 
 Dim thisDate2 As New DateTimeOffset(2011, 6, 10, 15, 24, 16, TimeSpan.Zero)
 Console.WriteLine("The current date and time: {0:MM/dd/yy H:mm:ss zzz}", 
                   thisDate2) 
 ' The example displays the following output:
'    Today is June 10, 2011.
'    The current date and time: 06/10/11 15:24:16 +00:00

在剖析作業中,自訂日期和時間格式字串可以搭配 DateTime.ParseExactDateTime.TryParseExactDateTimeOffset.ParseExactDateTimeOffset.TryParseExact 方法使用。In parsing operations, custom date and time format strings can be used with the DateTime.ParseExact, DateTime.TryParseExact, DateTimeOffset.ParseExact, and DateTimeOffset.TryParseExact methods. 這些方法需要輸入字串完全符合特定模式,剖析作業才會成功。These methods require that an input string conform exactly to a particular pattern for the parse operation to succeed. 下列範例說明呼叫 DateTimeOffset.ParseExact(String, String, IFormatProvider) 方法來剖析必須包含日、月和兩位數年的日期。The following example illustrates a call to the DateTimeOffset.ParseExact(String, String, IFormatProvider) method to parse a date that must include a day, a month, and a two-digit year.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] dateValues = { "30-12-2011", "12-30-2011", 
                              "30-12-11", "12-30-11" };
      string pattern = "MM-dd-yy";
      DateTime parsedDate;
      
      foreach (var dateValue in dateValues) {
         if (DateTime.TryParseExact(dateValue, pattern, null, 
                                   DateTimeStyles.None, out parsedDate))
            Console.WriteLine("Converted '{0}' to {1:d}.", 
                              dateValue, parsedDate);
         else
            Console.WriteLine("Unable to convert '{0}' to a date and time.", 
                              dateValue);
      }
   }
}
// The example displays the following output:
//    Unable to convert '30-12-2011' to a date and time.
//    Unable to convert '12-30-2011' to a date and time.
//    Unable to convert '30-12-11' to a date and time.
//    Converted '12-30-11' to 12/30/2011.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim dateValues() As String = { "30-12-2011", "12-30-2011", 
                                      "30-12-11", "12-30-11" }
      Dim pattern As String = "MM-dd-yy"
      Dim parsedDate As Date
      
      For Each dateValue As String In dateValues
         If DateTime.TryParseExact(dateValue, pattern, Nothing, 
                                   DateTimeStyles.None, parsedDate) Then
            Console.WriteLine("Converted '{0}' to {1:d}.", 
                              dateValue, parsedDate)
         Else
            Console.WriteLine("Unable to convert '{0}' to a date and time.", 
                              dateValue)
         End If                                                         
      Next
   End Sub
End Module
' The example displays the following output:
'    Unable to convert '30-12-2011' to a date and time.
'    Unable to convert '12-30-2011' to a date and time.
'    Unable to convert '30-12-11' to a date and time.
'    Converted '12-30-11' to 12/30/2011.

下表說明自訂日期和時間格式規範,並顯示每個格式規範所產生的結果字串。The following table describes the custom date and time format specifiers and displays a result string produced by each format specifier. 根據預設,結果字串會反映 en-US 文化特性的格式設定慣例。By default, result strings reflect the formatting conventions of the en-US culture. 如果特定格式規範會產生當地語系化的結果字串,則範例也會註明結果字串適用的文化特性。If a particular format specifier produces a localized result string, the example also notes the culture to which the result string applies. 如需使用自訂日期和時間格式字串的詳細資訊,請參閱注意一節。See the Notes section for additional information about using custom date and time format strings.

格式規範Format specifier 描述Description 範例Examples
"d""d" 月份天數,從 1 到 31。The day of the month, from 1 through 31.

詳細資訊:"d" 自訂格式規範More information: The "d" Custom Format Specifier.
2009-06-01T13:45:30 -> 12009-06-01T13:45:30 -> 1

2009-06-15T13:45:30 -> 152009-06-15T13:45:30 -> 15
"dd""dd" 月份天數,從 01 到 31。The day of the month, from 01 through 31.

詳細資訊:"dd" 自訂格式規範More information: The "dd" Custom Format Specifier.
2009-06-01T13:45:30 -> 012009-06-01T13:45:30 -> 01

2009-06-15T13:45:30 -> 152009-06-15T13:45:30 -> 15
"ddd""ddd" 星期幾的縮寫名稱。The abbreviated name of the day of the week.

詳細資訊:"ddd" 自訂格式規範More information: The "ddd" Custom Format Specifier.
2009-06-15T13:45:30 -> Mon (en-US)2009-06-15T13:45:30 -> Mon (en-US)

2009-06-15T13:45:30 -> Пн (ru-RU)2009-06-15T13:45:30 -> Пн (ru-RU)

2009-06-15T13:45:30 -> lun.2009-06-15T13:45:30 -> lun. (fr-FR)(fr-FR)
"dddd""dddd" 星期幾的完整名稱。The full name of the day of the week.

詳細資訊:"dddd" 自訂格式規範More information: The "dddd" Custom Format Specifier.
2009-06-15T13:45:30 -> Monday (en-US)2009-06-15T13:45:30 -> Monday (en-US)

2009-06-15T13:45:30 -> понедельник (ru-RU)2009-06-15T13:45:30 -> понедельник (ru-RU)

2009-06-15T13:45:30 -> lundi (fr-FR)2009-06-15T13:45:30 -> lundi (fr-FR)
"f""f" 日期和時間值中的十分之一秒。The tenths of a second in a date and time value.

詳細資訊:"f" 自訂格式規範More information: The "f" Custom Format Specifier.
2009-06-15T13:45:30.6170000 -> 62009-06-15T13:45:30.6170000 -> 6

2009-06-15T13:45:30.05 -> 02009-06-15T13:45:30.05 -> 0
"ff""ff" 日期和時間值中的百分之一秒。The hundredths of a second in a date and time value.

詳細資訊:"ff" 自訂格式規範More information: The "ff" Custom Format Specifier.
2009-06-15T13:45:30.6170000 -> 612009-06-15T13:45:30.6170000 -> 61

2009-06-15T13:45:30.0050000 -> 002009-06-15T13:45:30.0050000 -> 00
"fff""fff" 日期和時間值中的千分之一秒。The milliseconds in a date and time value.

詳細資訊:"fff" 自訂格式規範More information: The "fff" Custom Format Specifier.
6/15/2009 13:45:30.617 -> 6176/15/2009 13:45:30.617 -> 617

6/15/2009 13:45:30.0005 -> 0006/15/2009 13:45:30.0005 -> 000
"ffff""ffff" 日期和時間值中的萬分之一秒。The ten thousandths of a second in a date and time value.

詳細資訊:"ffff" 自訂格式規範More information: The "ffff" Custom Format Specifier.
2009-06-15T13:45:30.6175000 -> 61752009-06-15T13:45:30.6175000 -> 6175

2009-06-15T13:45:30.0000500 -> 00002009-06-15T13:45:30.0000500 -> 0000
"fffff""fffff" 日期和時間值中的十萬分之一秒。The hundred thousandths of a second in a date and time value.

詳細資訊:"fffff" 自訂格式規範More information: The "fffff" Custom Format Specifier.
2009-06-15T13:45:30.6175400 -> 617542009-06-15T13:45:30.6175400 -> 61754

6/15/2009 13:45:30.000005 -> 000006/15/2009 13:45:30.000005 -> 00000
"ffffff""ffffff" 日期和時間值中的百萬分之一秒。The millionths of a second in a date and time value.

詳細資訊:"ffffff" 自訂格式規範More information: The "ffffff" Custom Format Specifier.
2009-06-15T13:45:30.6175420 -> 6175422009-06-15T13:45:30.6175420 -> 617542

2009-06-15T13:45:30.0000005 -> 0000002009-06-15T13:45:30.0000005 -> 000000
"fffffff""fffffff" 日期和時間值中的千萬分之一秒。The ten millionths of a second in a date and time value.

詳細資訊:"fffffff" 自訂格式規範More information: The "fffffff" Custom Format Specifier.
2009-06-15T13:45:30.6175425 -> 61754252009-06-15T13:45:30.6175425 -> 6175425

2009-06-15T13:45:30.0001150 -> 00011502009-06-15T13:45:30.0001150 -> 0001150
"F""F" 如果不是零,則為日期和時間值中的十分之一秒。If non-zero, the tenths of a second in a date and time value.

詳細資訊:"F" 自訂格式規範More information: The "F" Custom Format Specifier.
2009-06-15T13:45:30.6170000 -> 62009-06-15T13:45:30.6170000 -> 6

2009-06-15T13:45:30.0500000 -> (沒有輸出)2009-06-15T13:45:30.0500000 -> (no output)
"FF""FF" 如果不是零,則為日期和時間值中的百分之一秒。If non-zero, the hundredths of a second in a date and time value.

詳細資訊:"FF" 自訂格式規範More information: The "FF" Custom Format Specifier.
2009-06-15T13:45:30.6170000 -> 612009-06-15T13:45:30.6170000 -> 61

2009-06-15T13:45:30.0050000 -> (沒有輸出)2009-06-15T13:45:30.0050000 -> (no output)
"FFF""FFF" 如果不是零,則為日期和時間值中的千分之一秒。If non-zero, the milliseconds in a date and time value.

詳細資訊:"FFF" 自訂格式規範More information: The "FFF" Custom Format Specifier.
2009-06-15T13:45:30.6170000 -> 6172009-06-15T13:45:30.6170000 -> 617

2009-06-15T13:45:30.0005000 -> (沒有輸出)2009-06-15T13:45:30.0005000 -> (no output)
"FFFF""FFFF" 如果不是零,則為日期和時間值中的萬分之一秒。If non-zero, the ten thousandths of a second in a date and time value.

詳細資訊:"FFFF" 自訂格式規範More information: The "FFFF" Custom Format Specifier.
2009-06-15T13:45:30.5275000 -> 52752009-06-15T13:45:30.5275000 -> 5275

2009-06-15T13:45:30.0000500 -> (沒有輸出)2009-06-15T13:45:30.0000500 -> (no output)
"FFFFF""FFFFF" 如果不是零,則為日期和時間值中的十萬分之一秒。If non-zero, the hundred thousandths of a second in a date and time value.

詳細資訊:"FFFFF" 自訂格式規範More information: The "FFFFF" Custom Format Specifier.
2009-06-15T13:45:30.6175400 -> 617542009-06-15T13:45:30.6175400 -> 61754

2009-06-15T13:45:30.0000050 -> (沒有輸出)2009-06-15T13:45:30.0000050 -> (no output)
"FFFFFF""FFFFFF" 如果不是零,則為日期和時間值中的百萬分之一秒。If non-zero, the millionths of a second in a date and time value.

詳細資訊:"FFFFFF" 自訂格式規範More information: The "FFFFFF" Custom Format Specifier.
2009-06-15T13:45:30.6175420 -> 6175422009-06-15T13:45:30.6175420 -> 617542

2009-06-15T13:45:30.0000005 -> (沒有輸出)2009-06-15T13:45:30.0000005 -> (no output)
"FFFFFFF""FFFFFFF" 如果不是零,則為日期和時間值中的千萬分之一秒。If non-zero, the ten millionths of a second in a date and time value.

詳細資訊:"FFFFFFF" 自訂格式規範More information: The "FFFFFFF" Custom Format Specifier.
2009-06-15T13:45:30.6175425 -> 61754252009-06-15T13:45:30.6175425 -> 6175425

2009-06-15T13:45:30.0001150 -> 0001152009-06-15T13:45:30.0001150 -> 000115
"g"、"gg""g", "gg" 週期或紀元。The period or era.

詳細資訊:"g" 或 "gg" 自訂格式規範More information: The "g" or "gg" Custom Format Specifier.
2009-06-15T13:45:30.6170000 -> A.D.2009-06-15T13:45:30.6170000 -> A.D.
"h""h" 採用 12 小時制的小時,從 1 到 12。The hour, using a 12-hour clock from 1 to 12.

詳細資訊:"h" 自訂格式規範More information: The "h" Custom Format Specifier.
2009-06-15T01:45:30 -> 12009-06-15T01:45:30 -> 1

2009-06-15T13:45:30 -> 12009-06-15T13:45:30 -> 1
"hh""hh" 採用 12 小時制的小時,從 01 到 12。The hour, using a 12-hour clock from 01 to 12.

詳細資訊:"hh" 自訂格式規範More information: The "hh" Custom Format Specifier.
2009-06-15T01:45:30 -> 012009-06-15T01:45:30 -> 01

2009-06-15T13:45:30 -> 012009-06-15T13:45:30 -> 01
"H""H" 採用 24 小時制的小時,從 0 到 23。The hour, using a 24-hour clock from 0 to 23.

詳細資訊:"H" 自訂格式規範More information: The "H" Custom Format Specifier.
2009-06-15T01:45:30 -> 12009-06-15T01:45:30 -> 1

2009-06-15T13:45:30 -> 132009-06-15T13:45:30 -> 13
"HH""HH" 採用 24 小時制的小時,從 00 到 23。The hour, using a 24-hour clock from 00 to 23.

詳細資訊:"HH" 自訂格式規範More information: The "HH" Custom Format Specifier.
2009-06-15T01:45:30 -> 012009-06-15T01:45:30 -> 01

2009-06-15T13:45:30 -> 132009-06-15T13:45:30 -> 13
"K""K" 時區資訊。Time zone information.

詳細資訊:"K" 自訂格式規範More information: The "K" Custom Format Specifier.
搭配 DateTime 值:With DateTime values:

2009-06-15T13:45:30, Kind Unspecified ->2009-06-15T13:45:30, Kind Unspecified ->

2009-06-15T13:45:30, Kind Utc -> Z2009-06-15T13:45:30, Kind Utc -> Z

2009-06-15T13:45:30, Kind Local -> -07:00 (依據本機電腦設定)2009-06-15T13:45:30, Kind Local -> -07:00 (depends on local computer settings)

搭配 DateTimeOffset 值:With DateTimeOffset values:

2009-06-15T01:45:30-07:00 --> -07:002009-06-15T01:45:30-07:00 --> -07:00

2009-06-15T08:45:30+00:00 --> +00:002009-06-15T08:45:30+00:00 --> +00:00
"m""m" 分鐘,從 0 到 59。The minute, from 0 through 59.

詳細資訊:"m" 自訂格式規範More information: The "m" Custom Format Specifier.
2009-06-15T01:09:30 -> 92009-06-15T01:09:30 -> 9

2009-06-15T13:29:30 -> 292009-06-15T13:29:30 -> 29
"mm""mm" 分鐘,從 00 到 59。The minute, from 00 through 59.

詳細資訊:"mm" 自訂格式規範More information: The "mm" Custom Format Specifier.
2009-06-15T01:09:30 -> 092009-06-15T01:09:30 -> 09

2009-06-15T01:45:30 -> 452009-06-15T01:45:30 -> 45
"M""M" 月份,從 1 到 12。The month, from 1 through 12.

詳細資訊:"M" 自訂格式規範More information: The "M" Custom Format Specifier.
2009-06-15T13:45:30 -> 62009-06-15T13:45:30 -> 6
"MM""MM" 月份,從 01 到 12。The month, from 01 through 12.

詳細資訊:"MM" 自訂格式規範More information: The "MM" Custom Format Specifier.
2009-06-15T13:45:30 -> 062009-06-15T13:45:30 -> 06
"MMM""MMM" 月份的縮寫名稱。The abbreviated name of the month.

詳細資訊:"MMM" 自訂格式規範More information: The "MMM" Custom Format Specifier.
2009-06-15T13:45:30 -> Jun (en-US)2009-06-15T13:45:30 -> Jun (en-US)

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

2009-06-15T13:45:30 -> Jun (zu-ZA)2009-06-15T13:45:30 -> Jun (zu-ZA)
"MMMM""MMMM" 月份的完整名稱。The full name of the month.

詳細資訊:"MMMM" 自訂格式規範More information: The "MMMM" Custom Format Specifier.
2009-06-15T13:45:30 -> June (en-US)2009-06-15T13:45:30 -> June (en-US)

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

2009-06-15T13:45:30 -> uJuni (zu-ZA)2009-06-15T13:45:30 -> uJuni (zu-ZA)
"s""s" 秒數,從 0 到 59。The second, from 0 through 59.

詳細資訊:"s" 自訂格式規範More information: The "s" Custom Format Specifier.
2009-06-15T13:45:09 -> 92009-06-15T13:45:09 -> 9
"ss""ss" 秒數,從 00 到 59。The second, from 00 through 59.

詳細資訊:"ss" 自訂格式規範More information: The "ss" Custom Format Specifier.
2009-06-15T13:45:09 -> 092009-06-15T13:45:09 -> 09
"t""t" AM/PM 指示項的第一個字元。The first character of the AM/PM designator.

詳細資訊:"t" 自訂格式規範More information: The "t" Custom Format Specifier.
2009-06-15T13:45:30 -> P (en-US)2009-06-15T13:45:30 -> P (en-US)

2009-06-15T13:45:30 -> 午 (ja-JP)2009-06-15T13:45:30 -> 午 (ja-JP)

2009-06-15T13:45:30 -> (fr-FR)2009-06-15T13:45:30 -> (fr-FR)
"tt""tt" AM/PM 指示項。The AM/PM designator.

詳細資訊:"tt" 自訂格式規範More information: The "tt" Custom Format Specifier.
2009-06-15T13:45:30 -> PM (en-US)2009-06-15T13:45:30 -> PM (en-US)

2009-06-15T13:45:30 -> 午後 (ja-JP)2009-06-15T13:45:30 -> 午後 (ja-JP)

2009-06-15T13:45:30 -> (fr-FR)2009-06-15T13:45:30 -> (fr-FR)
"y""y" 年份,從 0 到 99。The year, from 0 to 99.

詳細資訊:"y" 自訂格式規範More information: The "y" Custom Format Specifier.
0001-01-01T00:00:00 -> 10001-01-01T00:00:00 -> 1

0900-01-01T00:00:00 -> 00900-01-01T00:00:00 -> 0

1900-01-01T00:00:00 -> 01900-01-01T00:00:00 -> 0

2009-06-15T13:45:30 -> 92009-06-15T13:45:30 -> 9

2019-06-15T13:45:30 -> 192019-06-15T13:45:30 -> 19
"yy""yy" 年份,從 00 到 99。The year, from 00 to 99.

詳細資訊:"yy" 自訂格式規範More information: The "yy" Custom Format Specifier.
0001-01-01T00:00:00 -> 010001-01-01T00:00:00 -> 01

0900-01-01T00:00:00 -> 000900-01-01T00:00:00 -> 00

1900-01-01T00:00:00 -> 001900-01-01T00:00:00 -> 00

2019-06-15T13:45:30 -> 192019-06-15T13:45:30 -> 19
"yyy""yyy" 年份,至少三位數。The year, with a minimum of three digits.

詳細資訊:"yyy" 自訂格式規範More information: The "yyy" Custom Format Specifier.
0001-01-01T00:00:00 -> 0010001-01-01T00:00:00 -> 001

0900-01-01T00:00:00 -> 9000900-01-01T00:00:00 -> 900

1900-01-01T00:00:00 -> 19001900-01-01T00:00:00 -> 1900

2009-06-15T13:45:30 -> 20092009-06-15T13:45:30 -> 2009
"yyyy""yyyy" 以四位數表示的年份。The year as a four-digit number.

詳細資訊:"yyyy" 自訂格式規範More information: The "yyyy" Custom Format Specifier.
0001-01-01T00:00:00 -> 00010001-01-01T00:00:00 -> 0001

0900-01-01T00:00:00 -> 09000900-01-01T00:00:00 -> 0900

1900-01-01T00:00:00 -> 19001900-01-01T00:00:00 -> 1900

2009-06-15T13:45:30 -> 20092009-06-15T13:45:30 -> 2009
"yyyyy""yyyyy" 以五位數表示的年份。The year as a five-digit number.

詳細資訊:"yyyyy" 自訂格式規範More information: The "yyyyy" Custom Format Specifier.
0001-01-01T00:00:00 -> 000010001-01-01T00:00:00 -> 00001

2009-06-15T13:45:30 -> 020092009-06-15T13:45:30 -> 02009
"z""z" 與 UTC 相差的時數,不加上前置零。Hours offset from UTC, with no leading zeros.

詳細資訊:"z" 自訂格式規範More information: The "z" Custom Format Specifier.
2009-06-15T13:45:30-07:00 -> -72009-06-15T13:45:30-07:00 -> -7
"zz""zz" 與 UTC 相差的時數,單一位數值會加上前置零。Hours offset from UTC, with a leading zero for a single-digit value.

詳細資訊:"zz" 自訂格式規範More information: The "zz" Custom Format Specifier.
2009-06-15T13:45:30-07:00 -> -072009-06-15T13:45:30-07:00 -> -07
"zzz""zzz" 與 UTC 相差的時數和分鐘數。Hours and minutes offset from UTC.

詳細資訊:"zzz" 自訂格式規範More information: The "zzz" Custom Format Specifier.
2009-06-15T13:45:30-07:00 -> -07:002009-06-15T13:45:30-07:00 -> -07:00
":"":" 時間分隔符號。The time separator.

詳細資訊:":" 自訂格式規範More information: The ":" Custom Format Specifier.
2009-06-15T13:45:30 -> : (en-US)2009-06-15T13:45:30 -> : (en-US)

2009-06-15T13:45:30 -> .2009-06-15T13:45:30 -> . (it-IT)(it-IT)

2009-06-15T13:45:30 -> : (ja-JP)2009-06-15T13:45:30 -> : (ja-JP)
"/""/" 日期分隔符號。The date separator.

詳細資訊:"/" 自訂格式規範More Information: The "/" Custom Format Specifier.
2009-06-15T13:45:30 -> / (en-US)2009-06-15T13:45:30 -> / (en-US)

2009-06-15T13:45:30 -> - (ar-DZ)2009-06-15T13:45:30 -> - (ar-DZ)

2009-06-15T13:45:30 -> .2009-06-15T13:45:30 -> . (tr-TR)(tr-TR)
"string""string"

'string''string'
常值字串分隔符號。Literal string delimiter.

詳細資訊︰字元常值More information: Character literals.
2009-06-15T13:45:30 ("arr:" h:m t) -> arr: 1:45 P2009-06-15T13:45:30 ("arr:" h:m t) -> arr: 1:45 P

2009-06-15T13:45:30 ('arr:' h:m t) -> arr: 1:45 P2009-06-15T13:45:30 ('arr:' h:m t) -> arr: 1:45 P
% 將下列字元定義為自訂格式規範。Defines the following character as a custom format specifier.

詳細資訊:使用單一自訂格式規範More information:Using Single Custom Format Specifiers.
2009-06-15T13:45:30 (%h) -> 12009-06-15T13:45:30 (%h) -> 1
\|逸出字元。\|The escape character.

詳細資訊︰字元常值使用逸出字元More information: Character literals and Using the Escape Character.
2009-06-15T13:45:30 (h \h) -> 1 h2009-06-15T13:45:30 (h \h) -> 1 h
任意字元Any other character 字元會原封不動地複製到結果字串。The character is copied to the result string unchanged.

詳細資訊︰字元常值More information: Character literals.
2009-06-15T01:45:30 (arr hh:mm t) -> arr 01:45 A2009-06-15T01:45:30 (arr hh:mm t) -> arr 01:45 A

下列各節提供每個自訂日期和時間格式規範的其他資訊。The following sections provide additional information about each custom date and time format specifier. 除非特別註明,否則每個規範都會產生相同的字串表示,無論是與 DateTime 值或 DateTimeOffset 值搭配使用。Unless otherwise noted, each specifier produces an identical string representation regardless of whether it is used with a DateTime value or a DateTimeOffset value.

"d" 自訂格式規範The "d" custom format specifier

"d" 自訂格式規範以 1 到 31 的數字來表示月份天數。The "d" custom format specifier represents the day of the month as a number from 1 through 31. 單一位數的天數會格式化為沒有前置零的數字。A single-digit day is formatted without a leading zero.

如果單獨使用 "d" 格式規範,而沒有其他自訂格式規範,則會將它解譯為 "d" 標準日期和時間格式規範。If the "d" format specifier is used without other custom format specifiers, it is interpreted as the "d" standard date and time format specifier. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在幾個格式字串中加入 "d" 自訂格式規範。The following example includes the "d" custom format specifier in several format strings.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15); 

Console.WriteLine(date1.ToString("d, M", 
                  CultureInfo.InvariantCulture)); 
// Displays 29, 8

Console.WriteLine(date1.ToString("d MMMM", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays 29 August
Console.WriteLine(date1.ToString("d MMMM", 
                  CultureInfo.CreateSpecificCulture("es-MX")));
// Displays 29 agosto                                                
Dim date1 As Date = #08/29/2008 7:27:15PM#

Console.WriteLine(date1.ToString("d, M", _
                  CultureInfo.InvariantCulture)) 
' Displays 29, 8

Console.WriteLine(date1.ToString("d MMMM", _
                  CultureInfo.CreateSpecificCulture("en-US")))
' Displays 29 August
Console.WriteLine(date1.ToString("d MMMM", _
                  CultureInfo.CreateSpecificCulture("es-MX")))
' Displays 29 agosto                                                

回到表格Back to table

"dd" 自訂格式規範The "dd" custom format specifier

"dd" 自訂格式字串以 01 到 31 的數字來表示月份天數。The "dd" custom format string represents the day of the month as a number from 01 through 31. 單一位數的天數會格式化為有前置零的數字。A single-digit day is formatted with a leading zero.

下列範例會在自訂格式字串中加入 "dd" 自訂格式規範。The following example includes the "dd" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 1, 2, 6, 30, 15);

Console.WriteLine(date1.ToString("dd, MM", 
                  CultureInfo.InvariantCulture)); 
// 02, 01
Dim date1 As Date = #1/2/2008 6:30:15AM#

Console.WriteLine(date1.ToString("dd, MM", _
                  CultureInfo.InvariantCulture)) 
' 02, 01

回到表格Back to table

"ddd" 自訂格式規範The "ddd" custom format specifier

"ddd" 自訂格式規範表示星期幾的縮寫名稱。The "ddd" custom format specifier represents the abbreviated name of the day of the week. 星期幾的當地語系化縮寫名稱是擷取自目前或指定之文化特性的 DateTimeFormatInfo.AbbreviatedDayNames 屬性。The localized abbreviated name of the day of the week is retrieved from the DateTimeFormatInfo.AbbreviatedDayNames property of the current or specified culture.

下列範例會在自訂格式字串中加入 "ddd" 自訂格式規範。The following example includes the "ddd" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

Console.WriteLine(date1.ToString("ddd d MMM", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays Fri 29 Aug
Console.WriteLine(date1.ToString("ddd d MMM", 
                  CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays ven. 29 août                                                
Dim date1 As Date = #08/29/2008 7:27:15PM#

Console.WriteLine(date1.ToString("ddd d MMM", _
                  CultureInfo.CreateSpecificCulture("en-US")))
' Displays Fri 29 Aug
Console.WriteLine(date1.ToString("ddd d MMM", _
                  CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays ven. 29 août                                                

回到表格Back to table

"dddd" 自訂格式規範The "dddd" custom format specifier

"dddd" 自訂格式規範 (加上任意個額外的 "d" 規範) 表示星期幾的完整名稱。The "dddd" custom format specifier (plus any number of additional "d" specifiers) represents the full name of the day of the week. 星期幾的當地語系化名稱是擷取自目前或指定之文化特性的 DateTimeFormatInfo.DayNames 屬性。The localized name of the day of the week is retrieved from the DateTimeFormatInfo.DayNames property of the current or specified culture.

下列範例會在自訂格式字串中加入 "dddd" 自訂格式規範。The following example includes the "dddd" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

Console.WriteLine(date1.ToString("dddd dd MMMM", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays Friday 29 August
Console.WriteLine(date1.ToString("dddd dd MMMM", 
                  CultureInfo.CreateSpecificCulture("it-IT")));
// Displays venerdì 29 agosto                                
Dim date1 As Date = #08/29/2008 7:27:15PM#

Console.WriteLine(date1.ToString("dddd dd MMMM", _
                  CultureInfo.CreateSpecificCulture("en-US")))
' Displays Friday 29 August
Console.WriteLine(date1.ToString("dddd dd MMMM", _
                  CultureInfo.CreateSpecificCulture("it-IT")))
' Displays venerdì 29 agosto                                          

回到表格Back to table

"f" 自訂格式規範The "f" custom format specifier

"f" 自訂格式規範表示秒數的一位有效小數位數,也就是說,它表示日期和時間值中的十分之一秒。The "f" custom format specifier represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.

如果單獨使用 "f" 格式規範,而沒有其他格式規範,則會將它解譯為 "f" 標準日期和時間格式規範。If the "f" format specifier is used without other format specifiers, it is interpreted as the "f" standard date and time format specifier. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

當您使用 "f" 格式規範做為提供給 ParseExactTryParseExactParseExactTryParseExact 方法的一部分格式字串時,"f" 格式規範的數目表示秒數部分最重要的小數位數,必須存在此小數位數才能成功剖析字串。When you use "f" format specifiers as part of a format string supplied to the ParseExact, TryParseExact, ParseExact, or TryParseExact method, the number of "f" format specifiers indicates the number of most significant digits of the seconds fraction that must be present to successfully parse the string.

下列範例會在自訂格式字串中加入 "f" 自訂格式規範。The following example includes the "f" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci));
// Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci));
// Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci));
// Displays 07:27:15.018
Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 018)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci))
' Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci))
' Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci))
' Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci))
' Displays 07:27:15.018

回到表格Back to table

"ff" 自訂格式規範The "ff" custom format specifier

"ff" 自訂格式規範表示秒數的兩位有效小數位數,也就是說,它表示日期和時間值中的百分之一秒。The "ff" custom format specifier represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value.

下列範例會在自訂格式字串中加入 "ff" 自訂格式規範。following example includes the "ff" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci));
// Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci));
// Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci));
// Displays 07:27:15.018
Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 018)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci))
' Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci))
' Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci))
' Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci))
' Displays 07:27:15.018

回到表格Back to table

"fff" 自訂格式規範The "fff" custom format specifier

"fff" 自訂格式規範表示秒數的三位有效小數位數,也就是說,它表示日期和時間值中的千分之一秒。The "fff" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.

下列範例會在自訂格式字串中加入 "fff" 自訂格式規範。The following example includes the "fff" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci));
// Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci));
// Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci));
// Displays 07:27:15.018
Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 018)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci))
' Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci))
' Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci))
' Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci))
' Displays 07:27:15.018

回到表格Back to table

"ffff" 自訂格式規範The "ffff" custom format specifier

"ffff" 自訂格式規範表示秒數的四位有效小數位數,也就是說,它表示日期和時間值中的萬分之一秒。The "ffff" custom format specifier represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value.

雖然時間值的秒數部分可以顯示到萬分之一秒,但該值可能沒有太大的意義。Although it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. 日期和時間值的精確度會根據系統時鐘的解析度而定。The precision of date and time values depends on the resolution of the system clock. 在 Windows NT 3.5 (含) 以上版本和 Windows Vista 作業系統中,時鐘的解析度大約為 10-15 毫秒。On the Windows NT version 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

回到表格Back to table

"fffff" 自訂格式規範The "fffff" custom format specifier

"fffff" 自訂格式規範表示秒數的五位有效小數位數,也就是說,它表示日期和時間值中的十萬分之一秒。The "fffff" custom format specifier represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value.

雖然時間值的秒數部分可以顯示到十萬分之一秒,但該值可能沒有太大的意義。Although it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. 日期和時間值的精確度會根據系統時鐘的解析度而定。The precision of date and time values depends on the resolution of the system clock. 在 Windows NT 3.5 (含) 以上版本和 Windows Vista 作業系統中,時鐘的解析度大約為 10-15 毫秒。On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

回到表格Back to table

"ffffff" 自訂格式規範The "ffffff" custom format specifier

"ffffff" 自訂格式規範表示秒數的六位有效小數位數,也就是說,它表示日期和時間值中的百萬分之一秒。The "ffffff" custom format specifier represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value.

雖然時間值的秒數部分可以顯示到百萬分之一秒,但該值可能沒有太大的意義。Although it is possible to display the millionths of a second component of a time value, that value may not be meaningful. 日期和時間值的精確度會根據系統時鐘的解析度而定。The precision of date and time values depends on the resolution of the system clock. 在 Windows NT 3.5 (含) 以上版本和 Windows Vista 作業系統中,時鐘的解析度大約為 10-15 毫秒。On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

回到表格Back to table

"fffffff" 自訂格式規範The "fffffff" custom format specifier

"fffffff" 自訂格式規範表示秒數的七位有效小數位數,也就是說,它表示日期和時間值中的千萬分之一秒。The "fffffff" custom format specifier represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value.

雖然時間值的秒數部分可以顯示到千萬分之一秒,但該值可能沒有太大的意義。Although it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. 日期和時間值的精確度會根據系統時鐘的解析度而定。The precision of date and time values depends on the resolution of the system clock. 在 Windows NT 3.5 (含) 以上版本和 Windows Vista 作業系統中,時鐘的解析度大約為 10-15 毫秒。On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

回到表格Back to table

"F" 自訂格式規範The "F" custom format specifier

"F" 自訂格式規範表示秒數的一位有效小數位數,也就是說,它表示日期和時間值中的十分之一秒。The "F" custom format specifier represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value. 如果數字為零,則不會顯示任何內容。Nothing is displayed if the digit is zero.

如果單獨使用 "F" 格式規範,而沒有其他格式規範,則會將它解譯為 "F" 標準日期和時間格式規範。If the "F" format specifier is used without other format specifiers, it is interpreted as the "F" standard date and time format specifier. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

ParseExactTryParseExactParseExactTryParseExact 方法搭配使用的 "F" 格式規範數目,表示秒數部分最重要小數位數的上限,要成功剖析字串就不能超過此小數位數。The number of "F" format specifiers used with the ParseExact, TryParseExact, ParseExact, or TryParseExact method indicates the maximum number of most significant digits of the seconds fraction that can be present to successfully parse the string.

下列範例會在自訂格式字串中加入 "F" 自訂格式規範。The following example includes the "F" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci));
// Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci));
// Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci));
// Displays 07:27:15.018
Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 018)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci))
' Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci))
' Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci))
' Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci))
' Displays 07:27:15.018

回到表格Back to table

"FF" 自訂格式規範The "FF" custom format specifier

"FF" 自訂格式規範表示秒數的兩位有效小數位數,也就是說,它表示日期和時間值中的百分之一秒。The "FF" custom format specifier represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value. 不過,結尾的零或兩個零的數字都不會顯示。However, trailing zeros or two zero digits are not displayed.

下列範例會在自訂格式字串中加入 "FF" 自訂格式規範。The following example includes the "FF" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci));
// Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci));
// Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci));
// Displays 07:27:15.018
Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 018)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci))
' Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci))
' Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci))
' Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci))
' Displays 07:27:15.018

回到表格Back to table

"FFF" 自訂格式規範The "FFF" custom format specifier

"FFF" 自訂格式規範表示秒數的三位有效小數位數,也就是說,它表示日期和時間值中的千分之一秒。The "FFF" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value. 不過,結尾的零或三個零的數字都不會顯示。However, trailing zeros or three zero digits are not displayed.

下列範例會在自訂格式字串中加入 "FFF" 自訂格式規範。The following example includes the "FFF" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci));
// Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci));
// Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci));
// Displays 07:27:15.018
Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 018)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci))
' Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci))
' Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci))
' Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci))
' Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci))
' Displays 07:27:15.018

回到表格Back to table

"FFFF" 自訂格式規範The "FFFF" custom format specifier

"FFFF" 自訂格式規範表示秒數的四位有效小數位數,也就是說,它表示日期和時間值中的萬分之一秒。The "FFFF" custom format specifier represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value. 不過,結尾的零或四個零的數字都不會顯示。However, trailing zeros or four zero digits are not displayed.

雖然時間值的秒數部分可以顯示到萬分之一秒,但該值可能沒有太大的意義。Although it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. 日期和時間值的精確度會根據系統時鐘的解析度而定。The precision of date and time values depends on the resolution of the system clock. 在 Windows NT 3.5 (含) 以上版本和 Windows Vista 作業系統中,時鐘的解析度大約為 10-15 毫秒。On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

回到表格Back to table

"FFFFF" 自訂格式規範The "FFFFF" custom format specifier

"FFFFF" 自訂格式規範表示秒數的五位有效小數位數,也就是說,它表示日期和時間值中的十萬分之一秒。The "FFFFF" custom format specifier represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value. 不過,結尾的零或五個零的數字都不會顯示。However, trailing zeros or five zero digits are not displayed.

雖然時間值的秒數部分可以顯示到十萬分之一秒,但該值可能沒有太大的意義。Although it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. 日期和時間值的精確度會根據系統時鐘的解析度而定。The precision of date and time values depends on the resolution of the system clock. 在 Windows NT 3.5 (含) 以上版本和 Windows Vista 作業系統中,時鐘的解析度大約為 10-15 毫秒。On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

回到表格Back to table

"FFFFFF" 自訂格式規範The "FFFFFF" custom format specifier

"FFFFFF" 自訂格式規範表示秒數的六位有效小數位數,也就是說,它表示日期和時間值中的百萬分之一秒。The "FFFFFF" custom format specifier represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value. 不過,結尾的零或六個零的數字都不會顯示。However, trailing zeros or six zero digits are not displayed.

雖然時間值的秒數部分可以顯示到百萬分之一秒,但該值可能沒有太大的意義。Although it is possible to display the millionths of a second component of a time value, that value may not be meaningful. 日期和時間值的精確度會根據系統時鐘的解析度而定。The precision of date and time values depends on the resolution of the system clock. 在 Windows NT 3.5 (含) 以上版本和 Windows Vista 作業系統中,時鐘的解析度大約為 10-15 毫秒。On tfhe Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

回到表格Back to table

"FFFFFFF" 自訂格式規範The "FFFFFFF" custom format specifier

"FFFFFFF" 自訂格式規範表示秒數的七位有效小數位數,也就是說,它表示日期和時間值中的千萬分之一秒。The "FFFFFFF" custom format specifier represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value. 不過,結尾的零或七個零的數字都不會顯示。However, trailing zeros or seven zero digits are not displayed.

雖然時間值的秒數部分可以顯示到千萬分之一秒,但該值可能沒有太大的意義。Although it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. 日期和時間值的精確度會根據系統時鐘的解析度而定。The precision of date and time values depends on the resolution of the system clock. 在 Windows NT 3.5 (含) 以上版本和 Windows Vista 作業系統中,時鐘的解析度大約為 10-15 毫秒。On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

回到表格Back to table

"g" 或 "gg" 自訂格式規範The "g" or "gg" custom format specifier

"g" 或 "gg" 自訂格式規範 (加上任意個額外的 "g" 規範) 表示時期或時代,例如 A.D.。The "g" or "gg" custom format specifiers (plus any number of additional "g" specifiers) represents the period or era, such as A.D. 如果要格式化的日期沒有關聯的時期或時代字串,則格式化作業會忽略這個規範。The formatting operation ignores this specifier if the date to be formatted does not have an associated period or era string.

如果單獨使用 "g" 格式規範,而沒有其他自訂格式規範,則會將它解譯為 "g" 標準日期和時間格式規範。If the "g" format specifier is used without other custom format specifiers, it is interpreted as the "g" standard date and time format specifier. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在自訂格式字串中加入 "g" 自訂格式規範。The following example includes the "g" custom format specifier in a custom format string.

DateTime date1 = new DateTime(70, 08, 04);

Console.WriteLine(date1.ToString("MM/dd/yyyy g", 
                  CultureInfo.InvariantCulture));
// Displays 08/04/0070 A.D.                        
Console.WriteLine(date1.ToString("MM/dd/yyyy g", 
                  CultureInfo.CreateSpecificCulture("fr-FR")));                         
// Displays 08/04/0070 ap. J.-C.
Dim date1 As Date = #08/04/0070#

Console.WriteLine(date1.ToString("MM/dd/yyyy g", _
                  CultureInfo.InvariantCulture))
' Displays 08/04/0070 A.D.                        
Console.WriteLine(date1.ToString("MM/dd/yyyy g", _
                  CultureInfo.CreateSpecificCulture("fr-FR")))                         
' Displays 08/04/0070 ap. J.-C.

回到表格Back to table

"h" 自訂格式規範The "h" custom format specifier

"h" 自訂格式規範以 1 到 12 的數字來表示小時,也就是以 12 小時制來表示小時,從午夜或中午開始計算整點時數。The "h" custom format specifier represents the hour as a number from 1 through 12; that is, the hour is represented by a 12-hour clock that counts the whole hours since midnight or noon. 午夜後的特定小時與下午的相同小時會難以區分。A particular hour after midnight is indistinguishable from the same hour after noon. 小時是不進位的,而且單一位數的小時會格式化為沒有前置零的數字。The hour is not rounded, and a single-digit hour is formatted without a leading zero. 例如,假設時間為上午或下午 5:43,則此自訂格式規範會顯示 "5"。For example, given a time of 5:43 in the morning or afternoon, this custom format specifier displays "5".

如果單獨使用 "h" 格式規範,而沒有其他自訂格式規範,則會將它解譯為標準日期和時間格式規範,並擲回 FormatExceptionIf the "h" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在自訂格式字串中加入 "h" 自訂格式規範。The following example includes the "h" custom format specifier in a custom format string.

DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1 µ                        
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1.5 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1.5 µ
Dim date1 As Date 
date1 = #6:09:01PM#
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture))
' Displays 6:9:1 P
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 6:9:1 µ                        
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture))
' Displays 6:9:1.5 P
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 6:9:1.5 µ

回到表格Back to table

"hh" 自訂格式規範The "hh" custom format specifier

"hh" 自訂格式規範 (加上任意個額外的 "h" 規範) 以 01 到 12 的數字來表示小時,也就是以 12 小時制來表示小時,從午夜或中午開始計算整點時數。The "hh" custom format specifier (plus any number of additional "h" specifiers) represents the hour as a number from 01 through 12; that is, the hour is represented by a 12-hour clock that counts the whole hours since midnight or noon. 午夜後的特定小時與下午的相同小時會難以區分。A particular hour after midnight is indistinguishable from the same hour after noon. 小時是不進位的,而且單一位數的小時會格式化為有前置零的數字。The hour is not rounded, and a single-digit hour is formatted with a leading zero. 例如,假設時間為上午或下午 5:43,則此格式規範會顯示 "05"。For example, given a time of 5:43 in the morning or afternoon, this format specifier displays "05".

下列範例會在自訂格式字串中加入 "hh" 自訂格式規範。The following example includes the "hh" custom format specifier in a custom format string.

DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01 du.
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01.50 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", 
                  CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01.50 du.
Dim date1 As Date 
date1 = #6:09:01PM#
Console.WriteLine(date1.ToString("hh:mm:ss tt", _
                  CultureInfo.InvariantCulture))
' Displays 06:09:01 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss tt", _
                  CultureInfo.CreateSpecificCulture("hu-HU")))
' Displays 06:09:01 du.
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.InvariantCulture))
' Displays 06:09:01.50 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.CreateSpecificCulture("hu-HU")))
' Displays 06:09:01.50 du.

回到表格Back to table

"H" 自訂格式規範The "H" custom format specifier

"H" 自訂格式規範以 0 到 23 的數字來表示小時,也就是採用以零為起始的 24 小時制來表示小時,從午夜開始計算時數。The "H" custom format specifier represents the hour as a number from 0 through 23; that is, the hour is represented by a zero-based 24-hour clock that counts the hours since midnight. 單一位數的小時會格式化為沒有前置零的數字。A single-digit hour is formatted without a leading zero.

如果單獨使用 "H" 格式規範,而沒有其他自訂格式規範,則會將它解譯為標準日期和時間格式規範,並擲回 FormatExceptionIf the "H" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在自訂格式字串中加入 "H" 自訂格式規範。The following example includes the "H" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 1, 1, 6, 9, 1);
Console.WriteLine(date1.ToString("H:mm:ss", 
                  CultureInfo.InvariantCulture));
// Displays 6:09:01              
Dim date1 As Date = #6:09:01AM#
Console.WriteLine(date1.ToString("H:mm:ss", _
                  CultureInfo.InvariantCulture))
' Displays 6:09:01                        

回到表格Back to table

"HH" 自訂格式規範The "HH" custom format specifier

"HH" 自訂格式規範 (加上任意個額外的 "H" 規範) 以 00 到 23 的數字來表示小時,也就是採用以零為起始的 24 小時制來表示小時,從午夜開始計算時數。The "HH" custom format specifier (plus any number of additional "H" specifiers) represents the hour as a number from 00 through 23; that is, the hour is represented by a zero-based 24-hour clock that counts the hours since midnight. 單一位數的小時會格式化為具有前置零的數字。A single-digit hour is formatted with a leading zero.

下列範例會在自訂格式字串中加入 "HH" 自訂格式規範。The following example includes the "HH" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 1, 1, 6, 9, 1);
Console.WriteLine(date1.ToString("HH:mm:ss", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01                        
Dim date1 As Date = #6:09:01AM#
Console.WriteLine(date1.ToString("HH:mm:ss", _
                  CultureInfo.InvariantCulture))
' Displays 06:09:01                        

回到表格Back to table

"K" 自訂格式規範The "K" custom format specifier

"K" 自訂格式規範表示日期和時間值的時區資訊。The "K" custom format specifier represents the time zone information of a date and time value. DateTime 值搭配使用這個格式規範時,結果字串會由 DateTime.Kind 屬性的值定義:When this format specifier is used with DateTime values, the result string is defined by the value of the DateTime.Kind property:

若為 DateTimeOffset 值,"K" 格式規範相當於 "zzz" 格式規範,所產生的結果字串會包含 DateTimeOffset 值與 UTC 之間的時差。For DateTimeOffset values, the "K" format specifier is equivalent to the "zzz" format specifier, and produces a result string containing the DateTimeOffset value's offset from UTC.

如果單獨使用 "K" 格式規範,而沒有其他自訂格式規範,則會將它解譯為標準日期和時間格式規範,並擲回 FormatExceptionIf the "K" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會顯示美國太平洋時區中,"K" 自訂格式規範與各種 DateTimeDateTimeOffset 值搭配使用所產生的結果字串。The following example displays the string that results from using the "K" custom format specifier with various DateTime and DateTimeOffset values on a system in the U.S. Pacific Time zone.

Console.WriteLine(DateTime.Now.ToString("%K"));
// Displays -07:00
Console.WriteLine(DateTime.UtcNow.ToString("%K"));
// Displays Z      
Console.WriteLine("'{0}'", 
                  DateTime.SpecifyKind(DateTime.Now, 
                       DateTimeKind.Unspecified).ToString("%K"));
// Displays ''      
Console.WriteLine(DateTimeOffset.Now.ToString("%K"));
// Displays -07:00
Console.WriteLine(DateTimeOffset.UtcNow.ToString("%K"));
// Displays +00:00
Console.WriteLine(new DateTimeOffset(2008, 5, 1, 6, 30, 0, 
                      new TimeSpan(5, 0, 0)).ToString("%K"));
// Displays +05:00                        
Console.WriteLine(Date.Now.ToString("%K"))
' Displays -07:00
Console.WriteLine(Date.UtcNow.ToString("%K"))
' Displays Z      
Console.WriteLine("'{0}'", _
                  Date.SpecifyKind(Date.Now, _
                                   DateTimeKind.Unspecified). _
                  ToString("%K"))
' Displays ''      
Console.WriteLine(DateTimeOffset.Now.ToString("%K"))
' Displays -07:00
Console.WriteLine(DateTimeOffset.UtcNow.ToString("%K"))
' Displays +00:00
Console.WriteLine(New DateTimeOffset(2008, 5, 1, 6, 30, 0, _
                                     New TimeSpan(5, 0, 0)). _
                  ToString("%K"))
' Displays +05:00                        

回到表格Back to table

"m" 自訂格式規範The "m" custom format specifier

"m" 自訂格式規範以 0 到 59 的數字來表示分鐘。The "m" custom format specifier represents the minute as a number from 0 through 59. 分鐘表示自上個整點以來已經過的完整分鐘數。The minute represents whole minutes that have passed since the last hour. 單一位數的分鐘會格式化為沒有前置零的數字。A single-digit minute is formatted without a leading zero.

如果單獨使用 "m" 格式規範,而沒有其他自訂格式規範,則會將它解譯為 "m" 標準日期和時間格式規範。If the "m" format specifier is used without other custom format specifiers, it is interpreted as the "m" standard date and time format specifier. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在自訂格式字串中加入 "m" 自訂格式規範。The following example includes the "m" custom format specifier in a custom format string.

DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1 µ                        
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1.5 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1.5 µ
Dim date1 As Date 
date1 = #6:09:01PM#
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture))
' Displays 6:9:1 P
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 6:9:1 µ                        
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture))
' Displays 6:9:1.5 P
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 6:9:1.5 µ

回到表格Back to table

"mm" 自訂格式規範The "mm" custom format specifier

"mm" 自訂格式規範 (加上任意個額外的 "m" 規範) 以 00 到 59 的數字來表示分鐘。The "mm" custom format specifier (plus any number of additional "m" specifiers) represents the minute as a number from 00 through 59. 分鐘表示自上個整點以來已經過的完整分鐘數。The minute represents whole minutes that have passed since the last hour. 單一位數的分鐘會格式化為具有前置零的數字。A single-digit minute is formatted with a leading zero.

下列範例會在自訂格式字串中加入 "mm" 自訂格式規範。The following example includes the "mm" custom format specifier in a custom format string.

DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01 du.
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01.50 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", 
                  CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01.50 du.
Dim date1 As Date 
date1 = #6:09:01PM#
Console.WriteLine(date1.ToString("hh:mm:ss tt", _
                  CultureInfo.InvariantCulture))
' Displays 06:09:01 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss tt", _
                  CultureInfo.CreateSpecificCulture("hu-HU")))
' Displays 06:09:01 du.
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.InvariantCulture))
' Displays 06:09:01.50 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.CreateSpecificCulture("hu-HU")))
' Displays 06:09:01.50 du.

回到表格Back to table

"M" 自訂格式規範The "M" custom format specifier

"M" 自訂格式規範以 1 到 12 的數字來表示月份 (如果是有 13 個月的行事曆則從 1 到 13)。The "M" custom format specifier represents the month as a number from 1 through 12 (or from 1 through 13 for calendars that have 13 months). 單一位數的月份會格式化為沒有前置零的數字。A single-digit month is formatted without a leading zero.

如果單獨使用 "M" 格式規範,而沒有其他自訂格式規範,則會將它解譯為 "M" 標準日期和時間格式規範。If the "M" format specifier is used without other custom format specifiers, it is interpreted as the "M" standard date and time format specifier. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在自訂格式字串中加入 "M" 自訂格式規範。The following example includes the "M" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 18);
Console.WriteLine(date1.ToString("(M) MMM, MMMM", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays (8) Aug, August
Console.WriteLine(date1.ToString("(M) MMM, MMMM", 
                  CultureInfo.CreateSpecificCulture("nl-NL")));                       
// Displays (8) aug, augustus
Console.WriteLine(date1.ToString("(M) MMM, MMMM", 
                  CultureInfo.CreateSpecificCulture("lv-LV")));                        
// Displays (8) Aug, augusts                       
Dim date1 As Date = #8/18/2008#
Console.WriteLine(date1.ToString("(M) MMM, MMMM", _
                  CultureInfo.CreateSpecificCulture("en-US")))
' Displays (8) Aug, August
Console.WriteLine(date1.ToString("(M) MMM, MMMM", _
                  CultureInfo.CreateSpecificCulture("nl-NL")))                        
' Displays (8) aug, augustus
Console.WriteLine(date1.ToString("(M) MMM, MMMM", _
                  CultureInfo.CreateSpecificCulture("lv-LV")))                        
' Displays (8) Aug, augusts                       

回到表格Back to table

"MM" 自訂格式規範The "MM" custom format specifier

"MM" 自訂格式規範以 01 到 12 的數字來表示月份 (如果是有 13 個月的行事曆則從 1 到 13)。The "MM" custom format specifier represents the month as a number from 01 through 12 (or from 1 through 13 for calendars that have 13 months). 單一位數的月份會格式化為有前置零的數字。A single-digit month is formatted with a leading zero.

下列範例會在自訂格式字串中加入 "MM" 自訂格式規範。The following example includes the "MM" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 1, 2, 6, 30, 15);

Console.WriteLine(date1.ToString("dd, MM", 
                  CultureInfo.InvariantCulture)); 
// 02, 01
Dim date1 As Date = #1/2/2008 6:30:15AM#

Console.WriteLine(date1.ToString("dd, MM", _
                  CultureInfo.InvariantCulture)) 
' 02, 01

回到表格Back to table

"MMM" 自訂格式規範The "MMM" custom format specifier

"MMM" 自訂格式規範表示月份的縮寫名稱。The "MMM" custom format specifier represents the abbreviated name of the month. 月份的當地語系化縮寫名稱是擷取自目前或指定之文化特性的 DateTimeFormatInfo.AbbreviatedMonthNames 屬性。The localized abbreviated name of the month is retrieved from the DateTimeFormatInfo.AbbreviatedMonthNames property of the current or specified culture.

下列範例會在自訂格式字串中加入 "MMM" 自訂格式規範。The following example includes the "MMM" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

Console.WriteLine(date1.ToString("ddd d MMM", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays Fri 29 Aug
Console.WriteLine(date1.ToString("ddd d MMM", 
                  CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays ven. 29 août                                                
Dim date1 As Date = #08/29/2008 7:27:15PM#

Console.WriteLine(date1.ToString("ddd d MMM", _
                  CultureInfo.CreateSpecificCulture("en-US")))
' Displays Fri 29 Aug
Console.WriteLine(date1.ToString("ddd d MMM", _
                  CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays ven. 29 août                                                

回到表格Back to table

"MMMM" 自訂格式規範The "MMMM" custom format specifier

"MMMM" 自訂格式規範表示月份的完整名稱。The "MMMM" custom format specifier represents the full name of the month. 月份的當地語系化名稱是擷取自目前或指定之文化特性的 DateTimeFormatInfo.MonthNames 屬性。The localized name of the month is retrieved from the DateTimeFormatInfo.MonthNames property of the current or specified culture.

下列範例會在自訂格式字串中加入 "MMMM" 自訂格式規範。The following example includes the "MMMM" custom format specifier in a custom format string.

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

Console.WriteLine(date1.ToString("dddd dd MMMM", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays Friday 29 August
Console.WriteLine(date1.ToString("dddd dd MMMM", 
                  CultureInfo.CreateSpecificCulture("it-IT")));
// Displays venerdì 29 agosto                                
Dim date1 As Date = #08/29/2008 7:27:15PM#

Console.WriteLine(date1.ToString("dddd dd MMMM", _
                  CultureInfo.CreateSpecificCulture("en-US")))
' Displays Friday 29 August
Console.WriteLine(date1.ToString("dddd dd MMMM", _
                  CultureInfo.CreateSpecificCulture("it-IT")))
' Displays venerdì 29 agosto                                          

回到表格Back to table

"s" 自訂格式規範The "s" custom format specifier

"s" 自訂格式規範以 0 到 59 的數字來表示秒數。The "s" custom format specifier represents the seconds as a number from 0 through 59. 結果表示自上個分鐘以來已經過的完整秒數。The result represents whole seconds that have passed since the last minute. 單一位數的秒數會格式化為沒有前置零的數字。A single-digit second is formatted without a leading zero.

如果單獨使用 "s" 格式規範,而沒有其他自訂格式規範,則會將它解譯為 "s" 標準日期和時間格式規範。If the "s" format specifier is used without other custom format specifiers, it is interpreted as the "s" standard date and time format specifier. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在自訂格式字串中加入 "s" 自訂格式規範。The following example includes the "s" custom format specifier in a custom format string.

DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1 µ                        
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1.5 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1.5 µ
Dim date1 As Date 
date1 = #6:09:01PM#
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture))
' Displays 6:9:1 P
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 6:9:1 µ                        
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture))
' Displays 6:9:1.5 P
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 6:9:1.5 µ

回到表格Back to table

"ss" 自訂格式規範The "ss" custom format specifier

"ss" 自訂格式規範 (加上任意個額外的 "s" 規範) 以 00 到 59 的數字來表示秒數。The "ss" custom format specifier (plus any number of additional "s" specifiers) represents the seconds as a number from 00 through 59. 結果表示自上個分鐘以來已經過的完整秒數。The result represents whole seconds that have passed since the last minute. 單一位數的秒數會格式化為具有前置零的數字。A single-digit second is formatted with a leading zero.

下列範例會在自訂格式字串中加入 "ss" 自訂格式規範。The following example includes the "ss" custom format specifier in a custom format string.

DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01 du.
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01.50 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", 
                  CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01.50 du.
Dim date1 As Date 
date1 = #6:09:01PM#
Console.WriteLine(date1.ToString("hh:mm:ss tt", _
                  CultureInfo.InvariantCulture))
' Displays 06:09:01 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss tt", _
                  CultureInfo.CreateSpecificCulture("hu-HU")))
' Displays 06:09:01 du.
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.InvariantCulture))
' Displays 06:09:01.50 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.CreateSpecificCulture("hu-HU")))
' Displays 06:09:01.50 du.

回到表格Back to table

"t" 自訂格式規範The "t" custom format specifier

"t" 自訂格式規範表示 AM/PM 指示項的第一個字元。The "t" custom format specifier represents the first character of the AM/PM designator. 適當的當地語系化指示項是擷取自目前或特定文化特性的 DateTimeFormatInfo.AMDesignatorDateTimeFormatInfo.PMDesignator 屬性。The appropriate localized designator is retrieved from the DateTimeFormatInfo.AMDesignator or DateTimeFormatInfo.PMDesignator property of the current or specific culture. AM 指示項用於從 0:00:00 (午夜) 到 11:59:59.999 為止的所有時間。The AM designator is used for all times from 0:00:00 (midnight) to 11:59:59.999. PM 指示項用於從 12:00:00 (中午) 到 23:59:59.999 為止的所有時間。The PM designator is used for all times from 12:00:00 (noon) to 23:59:59.999.

如果單獨使用 "t" 格式規範,而沒有其他自訂格式規範,則會將它解譯為 "t" 標準日期和時間格式規範。If the "t" format specifier is used without other custom format specifiers, it is interpreted as the "t" standard date and time format specifier. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在自訂格式字串中加入 "t" 自訂格式規範。The following example includes the "t" custom format specifier in a custom format string.

DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1 µ                        
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1.5 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1.5 µ
Dim date1 As Date 
date1 = #6:09:01PM#
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture))
' Displays 6:9:1 P
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 6:9:1 µ                        
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture))
' Displays 6:9:1.5 P
Console.WriteLine(date1.ToString("h:m:s.F t", _
                  CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 6:9:1.5 µ

回到表格Back to table

"tt" 自訂格式規範The "tt" custom format specifier

"tt" 自訂格式規範 (加上任意個額外的 "t" 規範) 表示整個 AM/PM 指示項。The "tt" custom format specifier (plus any number of additional "t" specifiers) represents the entire AM/PM designator. 適當的當地語系化指示項是擷取自目前或特定文化特性的 DateTimeFormatInfo.AMDesignatorDateTimeFormatInfo.PMDesignator 屬性。The appropriate localized designator is retrieved from the DateTimeFormatInfo.AMDesignator or DateTimeFormatInfo.PMDesignator property of the current or specific culture. AM 指示項用於從 0:00:00 (午夜) 到 11:59:59.999 為止的所有時間。The AM designator is used for all times from 0:00:00 (midnight) to 11:59:59.999. PM 指示項用於從 12:00:00 (中午) 到 23:59:59.999 為止的所有時間。The PM designator is used for all times from 12:00:00 (noon) to 23:59:59.999.

對於需要特別強調 AM 與 PM 的語言,請務必使用 "tt" 規範。Make sure to use the "tt" specifier for languages for which it is necessary to maintain the distinction between AM and PM. 其中一種就是日文,日文的 AM 和 PM 指示項的第二個字元 (非第一個字元) 不同。An example is Japanese, for which the AM and PM designators differ in the second character instead of the first character.

下列範例會在自訂格式字串中加入 "tt" 自訂格式規範。The following example includes the "tt" custom format specifier in a custom format string.

DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss tt", 
                  CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01 du.
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", 
                  CultureInfo.InvariantCulture));
// Displays 06:09:01.50 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", 
                  CultureInfo.CreateSpecificCulture("hu-HU")));
// Displays 06:09:01.50 du.
Dim date1 As Date 
date1 = #6:09:01PM#
Console.WriteLine(date1.ToString("hh:mm:ss tt", _
                  CultureInfo.InvariantCulture))
' Displays 06:09:01 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss tt", _
                  CultureInfo.CreateSpecificCulture("hu-HU")))
' Displays 06:09:01 du.
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.InvariantCulture))
' Displays 06:09:01.50 PM                        
Console.WriteLine(date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.CreateSpecificCulture("hu-HU")))
' Displays 06:09:01.50 du.

回到表格Back to table

"y" 自訂格式規範The "y" custom format specifier

"y" 自訂格式規範以一位或兩位數來表示年份。The "y" custom format specifier represents the year as a one-digit or two-digit number. 如果年份超過兩個位數,結果中只會出現兩個低位數字。If the year has more than two digits, only the two low-order digits appear in the result. 如果兩位數年份的第一個數字以零開始 (例如 2008),則會格式化為沒有前置零的數字。If the first digit of a two-digit year begins with a zero (for example, 2008), the number is formatted without a leading zero.

如果單獨使用 "y" 格式規範,而沒有其他自訂格式規範,則會將它解譯為 "y" 標準日期和時間格式規範。If the "y" format specifier is used without other custom format specifiers, it is interpreted as the "y" standard date and time format specifier. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在自訂格式字串中加入 "y" 自訂格式規範。The following example includes the "y" custom format specifier in a custom format string.

DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
Console.WriteLine(date1.ToString("%y"));
// Displays 1
Console.WriteLine(date1.ToString("yy"));
// Displays 01
Console.WriteLine(date1.ToString("yyy"));
// Displays 001
Console.WriteLine(date1.ToString("yyyy"));
// Displays 0001
Console.WriteLine(date1.ToString("yyyyy"));
// Displays 00001
Console.WriteLine(date2.ToString("%y"));
// Displays 10
Console.WriteLine(date2.ToString("yy"));
// Displays 10
Console.WriteLine(date2.ToString("yyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"));
// Displays 02010      
Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
Console.WriteLine(date1.ToString("%y"))
' Displays 1
Console.WriteLine(date1.ToString("yy"))
' Displays 01
Console.WriteLine(date1.ToString("yyy"))
' Displays 001
Console.WriteLine(date1.ToString("yyyy"))
' Displays 0001
Console.WriteLine(date1.ToString("yyyyy"))
' Displays 00001
Console.WriteLine(date2.ToString("%y"))
' Displays 10
Console.WriteLine(date2.ToString("yy"))
' Displays 10
Console.WriteLine(date2.ToString("yyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"))
' Displays 02010      

回到表格Back to table

"yy" 自訂格式規範The "yy" custom format specifier

"yy" 自訂格式規範以兩位數來表示年份。The "yy" custom format specifier represents the year as a two-digit number. 如果年份超過兩個位數,結果中只會出現兩個低位數字。If the year has more than two digits, only the two low-order digits appear in the result. 如果兩位數年份少於兩個有效位數,則會以前置零填補此數字來產生兩位數。If the two-digit year has fewer than two significant digits, the number is padded with leading zeros to produce two digits.

在剖析作業中,使用 "yy" 自訂格式規範所剖析的兩位數年份,是根據格式提供者現行曆法的 Calendar.TwoDigitYearMax 屬性進行解譯。In a parsing operation, a two-digit year that is parsed using the "yy" custom format specifier is interpreted based on the Calendar.TwoDigitYearMax property of the format provider's current calendar. 下列範例會使用 en-US 文化特性的預設西曆 (在此案例中為目前的文化特性),剖析具有兩位數年份之日期的字串表示。The following example parses the string representation of a date that has a two-digit year by using the default Gregorian calendar of the en-US culture, which, in this case, is the current culture. 接著它會將目前文化特性的 CultureInfo 物件變更為使用已修改 GregorianCalendar 屬性的 TwoDigitYearMax 物件。It then changes the current culture's CultureInfo object to use a GregorianCalendar object whose TwoDigitYearMax property has been modified.

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      string fmt = "dd-MMM-yy";
      string value = "24-Jan-49";
      
      Calendar cal = (Calendar) CultureInfo.CurrentCulture.Calendar.Clone();
      Console.WriteLine("Two Digit Year Range: {0} - {1}", 
                        cal.TwoDigitYearMax - 99, cal.TwoDigitYearMax);
      
      Console.WriteLine("{0:d}", DateTime.ParseExact(value, fmt, null));
      Console.WriteLine();
      
      cal.TwoDigitYearMax = 2099;
      CultureInfo culture = (CultureInfo) CultureInfo.CurrentCulture.Clone();
      culture.DateTimeFormat.Calendar = cal;
      Thread.CurrentThread.CurrentCulture = culture;

      Console.WriteLine("Two Digit Year Range: {0} - {1}", 
                        cal.TwoDigitYearMax - 99, cal.TwoDigitYearMax);
      Console.WriteLine("{0:d}", DateTime.ParseExact(value, fmt, null));
   }
}
// The example displays the following output:
//       Two Digit Year Range: 1930 - 2029
//       1/24/1949
//       
//       Two Digit Year Range: 2000 - 2099
//       1/24/2049
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim fmt As String = "dd-MMM-yy"
      Dim value As String = "24-Jan-49"
      
      Dim cal As Calendar = CType(CultureInfo.CurrentCulture.Calendar.Clone(), Calendar)
      Console.WriteLine("Two Digit Year Range: {0} - {1}", 
                        cal.TwoDigitYearMax - 99, cal.TwoDigitYearMax)
      
      Console.WriteLine("{0:d}", DateTime.ParseExact(value, fmt, Nothing))
      Console.WriteLine()
      
      cal.TwoDigitYearMax = 2099
      Dim culture As CultureInfo = CType(CultureInfo.CurrentCulture.Clone(), CultureInfo)
      culture.DateTimeFormat.Calendar = cal
      Thread.CurrentThread.CurrentCulture = culture

      Console.WriteLine("Two Digit Year Range: {0} - {1}", 
                        cal.TwoDigitYearMax - 99, cal.TwoDigitYearMax)
      Console.WriteLine("{0:d}", DateTime.ParseExact(value, fmt, Nothing))
   End Sub
End Module
' The example displays the following output:
'       Two Digit Year Range: 1930 - 2029
'       1/24/1949
'       
'       Two Digit Year Range: 2000 - 2099
'       1/24/2049

下列範例會在自訂格式字串中加入 "yy" 自訂格式規範。The following example includes the "yy" custom format specifier in a custom format string.

DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
Console.WriteLine(date1.ToString("%y"));
// Displays 1
Console.WriteLine(date1.ToString("yy"));
// Displays 01
Console.WriteLine(date1.ToString("yyy"));
// Displays 001
Console.WriteLine(date1.ToString("yyyy"));
// Displays 0001
Console.WriteLine(date1.ToString("yyyyy"));
// Displays 00001
Console.WriteLine(date2.ToString("%y"));
// Displays 10
Console.WriteLine(date2.ToString("yy"));
// Displays 10
Console.WriteLine(date2.ToString("yyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"));
// Displays 02010      
Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
Console.WriteLine(date1.ToString("%y"))
' Displays 1
Console.WriteLine(date1.ToString("yy"))
' Displays 01
Console.WriteLine(date1.ToString("yyy"))
' Displays 001
Console.WriteLine(date1.ToString("yyyy"))
' Displays 0001
Console.WriteLine(date1.ToString("yyyyy"))
' Displays 00001
Console.WriteLine(date2.ToString("%y"))
' Displays 10
Console.WriteLine(date2.ToString("yy"))
' Displays 10
Console.WriteLine(date2.ToString("yyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"))
' Displays 02010      

回到表格Back to table

"yyy" 自訂格式規範The "yyy" custom format specifier

"yyy" 自訂格式規範以最少三位數來表示年份。The "yyy" custom format specifier represents the year with a minimum of three digits. 如果年份超過三個有效位數,它們會包含在結果字串中。If the year has more than three significant digits, they are included in the result string. 如果年份少於三個位數,則會以前置零填補此數字來產生三位數。If the year has fewer than three digits, the number is padded with leading zeros to produce three digits.

注意

對於可具有五位數年份的泰國佛教曆法,此格式規範會顯示全部有效位數。For the Thai Buddhist calendar, which can have five-digit years, this format specifier displays all significant digits.

下列範例會在自訂格式字串中加入 "yyy" 自訂格式規範。The following example includes the "yyy" custom format specifier in a custom format string.

DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
Console.WriteLine(date1.ToString("%y"));
// Displays 1
Console.WriteLine(date1.ToString("yy"));
// Displays 01
Console.WriteLine(date1.ToString("yyy"));
// Displays 001
Console.WriteLine(date1.ToString("yyyy"));
// Displays 0001
Console.WriteLine(date1.ToString("yyyyy"));
// Displays 00001
Console.WriteLine(date2.ToString("%y"));
// Displays 10
Console.WriteLine(date2.ToString("yy"));
// Displays 10
Console.WriteLine(date2.ToString("yyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"));
// Displays 02010      
Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
Console.WriteLine(date1.ToString("%y"))
' Displays 1
Console.WriteLine(date1.ToString("yy"))
' Displays 01
Console.WriteLine(date1.ToString("yyy"))
' Displays 001
Console.WriteLine(date1.ToString("yyyy"))
' Displays 0001
Console.WriteLine(date1.ToString("yyyyy"))
' Displays 00001
Console.WriteLine(date2.ToString("%y"))
' Displays 10
Console.WriteLine(date2.ToString("yy"))
' Displays 10
Console.WriteLine(date2.ToString("yyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"))
' Displays 02010      

回到表格Back to table

"yyyy" 自訂格式規範The "yyyy" custom format specifier

"yyyy" 自訂格式規範以最少四位數來表示年份。The "yyyy" custom format specifier represents the year with a minimum of four digits. 如果年份超過四個有效位數,它們會包含在結果字串中。If the year has more than four significant digits, they are included in the result string. 如果年份少於四個位數,則會以前置零填補此數字,以產生四個位數。If the year has fewer than four digits, the number is padded with leading zeros to produce four digits.

注意

對於可具有五位數年份的泰國佛教曆法,此格式規範會顯示最少四個位數。For the Thai Buddhist calendar, which can have five-digit years, this format specifier displays a minimum of four digits.

下列範例會在自訂格式字串中加入 "yyyy" 自訂格式規範。The following example includes the "yyyy" custom format specifier in a custom format string.

DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
Console.WriteLine(date1.ToString("%y"));
// Displays 1
Console.WriteLine(date1.ToString("yy"));
// Displays 01
Console.WriteLine(date1.ToString("yyy"));
// Displays 001
Console.WriteLine(date1.ToString("yyyy"));
// Displays 0001
Console.WriteLine(date1.ToString("yyyyy"));
// Displays 00001
Console.WriteLine(date2.ToString("%y"));
// Displays 10
Console.WriteLine(date2.ToString("yy"));
// Displays 10
Console.WriteLine(date2.ToString("yyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"));
// Displays 02010      
Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
Console.WriteLine(date1.ToString("%y"))
' Displays 1
Console.WriteLine(date1.ToString("yy"))
' Displays 01
Console.WriteLine(date1.ToString("yyy"))
' Displays 001
Console.WriteLine(date1.ToString("yyyy"))
' Displays 0001
Console.WriteLine(date1.ToString("yyyyy"))
' Displays 00001
Console.WriteLine(date2.ToString("%y"))
' Displays 10
Console.WriteLine(date2.ToString("yy"))
' Displays 10
Console.WriteLine(date2.ToString("yyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"))
' Displays 02010      

回到表格Back to table

"yyyyy" 自訂格式規範The "yyyyy" custom format specifier

"yyyyy" 自訂格式規範 (加上任意個額外的 "y" 規範) 以最少五位數來表示年份。The "yyyyy" custom format specifier (plus any number of additional "y" specifiers) represents the year with a minimum of five digits. 如果年份超過五個有效位數,它們會包含在結果字串中。If the year has more than five significant digits, they are included in the result string. 如果年份少於五位數,則會以前置零填補此數字來產生五位數。If the year has fewer than five digits, the number is padded with leading zeros to produce five digits.

如果有更多 "y" 規範,則會視需要以前置零填補此數字,以產生該 "y" 規範數目的位數。If there are additional "y" specifiers, the number is padded with as many leading zeros as necessary to produce the number of "y" specifiers.

下列範例會在自訂格式字串中加入 "yyyyy" 自訂格式規範。The following example includes the "yyyyy" custom format specifier in a custom format string.

DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
Console.WriteLine(date1.ToString("%y"));
// Displays 1
Console.WriteLine(date1.ToString("yy"));
// Displays 01
Console.WriteLine(date1.ToString("yyy"));
// Displays 001
Console.WriteLine(date1.ToString("yyyy"));
// Displays 0001
Console.WriteLine(date1.ToString("yyyyy"));
// Displays 00001
Console.WriteLine(date2.ToString("%y"));
// Displays 10
Console.WriteLine(date2.ToString("yy"));
// Displays 10
Console.WriteLine(date2.ToString("yyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyy"));
// Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"));
// Displays 02010      
Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
Console.WriteLine(date1.ToString("%y"))
' Displays 1
Console.WriteLine(date1.ToString("yy"))
' Displays 01
Console.WriteLine(date1.ToString("yyy"))
' Displays 001
Console.WriteLine(date1.ToString("yyyy"))
' Displays 0001
Console.WriteLine(date1.ToString("yyyyy"))
' Displays 00001
Console.WriteLine(date2.ToString("%y"))
' Displays 10
Console.WriteLine(date2.ToString("yy"))
' Displays 10
Console.WriteLine(date2.ToString("yyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyy"))
' Displays 2010      
Console.WriteLine(date2.ToString("yyyyy"))
' Displays 02010      

回到表格Back to table

"z" 自訂格式規範The "z" custom format specifier

搭配 DateTime 值使用時,"z" 自訂格式規範表示本地作業系統時區與國際標準時間 (UTC) 之間的時差 (帶正負號),以小時為單位。With DateTime values, the "z" custom format specifier represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. 它不會反映執行個體的 DateTime.Kind 屬性值。It does not reflect the value of an instance's DateTime.Kind property. 因此,不建議將 "z" 格式規範搭配 DateTime 值使用。For this reason, the "z" format specifier is not recommended for use with DateTime values.

搭配 DateTimeOffset 值使用時,此格式規範表示 DateTimeOffset 值與 UTC 之間的時差,以小時為單位。With DateTimeOffset values, this format specifier represents the DateTimeOffset value's offset from UTC in hours.

顯示時差時,一定會有前置正負號。The offset is always displayed with a leading sign. 加號 (+) 表示早於 UTC 的時數,減號 (-) 表示晚於 UTC 的時數。A plus sign (+) indicates hours ahead of UTC, and a minus sign (-) indicates hours behind UTC. 單一位數的時差會格式化為沒有前置零的數字。A single-digit offset is formatted without a leading zero.

如果單獨使用 "z" 格式規範,而沒有其他自訂格式規範,則會將它解譯為標準日期和時間格式規範,並擲回 FormatExceptionIf the "z" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

下列範例會在自訂格式字串中加入 "z" 自訂格式規範。The following example includes the "z" custom format specifier in a custom format string.

DateTime date1 = DateTime.UtcNow;
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", 
                  date1));
// Displays -7, -07, -07:00                     

DateTimeOffset date2 = new DateTimeOffset(2008, 8, 1, 0, 0, 0, 
                                          new TimeSpan(6, 0, 0));
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", 
                  date2));
// Displays +6, +06, +06:00
Dim date1 As Date = Date.UtcNow
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date1))
' Displays -7, -07, -07:00                     

Dim date2 As New DateTimeOffset(2008, 8, 1, 0, 0, 0, _
                                New Timespan(6, 0, 0))
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date2))                                     
' Displays +6, +06, +06:00

回到表格Back to table

"zz" 自訂格式規範The "zz" custom format specifier

搭配 DateTime 值使用時,"zz" 自訂格式規範表示本地作業系統時區與 UTC 之間的時差 (帶正負號),以小時為單位。With DateTime values, the "zz" custom format specifier represents the signed offset of the local operating system's time zone from UTC, measured in hours. 它不會反映執行個體的 DateTime.Kind 屬性值。It does not reflect the value of an instance's DateTime.Kind property. 因此,不建議將 "zz" 格式規範搭配 DateTime 值使用。For this reason, the "zz" format specifier is not recommended for use with DateTime values.

搭配 DateTimeOffset 值使用時,此格式規範表示 DateTimeOffset 值與 UTC 之間的時差,以小時為單位。With DateTimeOffset values, this format specifier represents the DateTimeOffset value's offset from UTC in hours.

顯示時差時,一定會有前置正負號。The offset is always displayed with a leading sign. 加號 (+) 表示早於 UTC 的時數,減號 (-) 表示晚於 UTC 的時數。A plus sign (+) indicates hours ahead of UTC, and a minus sign (-) indicates hours behind UTC. 單一位數的時差會格式化為有前置零的數字。A single-digit offset is formatted with a leading zero.

下列範例會在自訂格式字串中加入 "zz" 自訂格式規範。The following example includes the "zz" custom format specifier in a custom format string.

DateTime date1 = DateTime.UtcNow;
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", 
                  date1));
// Displays -7, -07, -07:00                     

DateTimeOffset date2 = new DateTimeOffset(2008, 8, 1, 0, 0, 0, 
                                          new TimeSpan(6, 0, 0));
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", 
                  date2));
// Displays +6, +06, +06:00
Dim date1 As Date = Date.UtcNow
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date1))
' Displays -7, -07, -07:00                     

Dim date2 As New DateTimeOffset(2008, 8, 1, 0, 0, 0, _
                                New Timespan(6, 0, 0))
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date2))                                     
' Displays +6, +06, +06:00

回到表格Back to table

"zzz" 自訂格式規範The "zzz" custom format specifier

搭配 DateTime 值使用時,"zzz" 自訂格式規範表示本地作業系統時區與 UTC 之間的時差 (帶正負號),以小時和分鐘為單位。With DateTime values, the "zzz" custom format specifier represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. 它不會反映執行個體的 DateTime.Kind 屬性值。It does not reflect the value of an instance's DateTime.Kind property. 因此,不建議將 "zzz" 格式規範搭配 DateTime 值使用。For this reason, the "zzz" format specifier is not recommended for use with DateTime values.

搭配 DateTimeOffset 值使用時,此格式規範表示 DateTimeOffset 值與 UTC 之間的時差,以小時和分鐘為單位。With DateTimeOffset values, this format specifier represents the DateTimeOffset value's offset from UTC in hours and minutes.

顯示時差時,一定會有前置正負號。The offset is always displayed with a leading sign. 加號 (+) 表示早於 UTC 的時數,減號 (-) 表示晚於 UTC 的時數。A plus sign (+) indicates hours ahead of UTC, and a minus sign (-) indicates hours behind UTC. 單一位數的時差會格式化為有前置零的數字。A single-digit offset is formatted with a leading zero.

下列範例會在自訂格式字串中加入 "zzz" 自訂格式規範。The following example includes the "zzz" custom format specifier in a custom format string.

DateTime date1 = DateTime.UtcNow;
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", 
                  date1));
// Displays -7, -07, -07:00                     

DateTimeOffset date2 = new DateTimeOffset(2008, 8, 1, 0, 0, 0, 
                                          new TimeSpan(6, 0, 0));
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", 
                  date2));
// Displays +6, +06, +06:00
Dim date1 As Date = Date.UtcNow
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date1))
' Displays -7, -07, -07:00                     

Dim date2 As New DateTimeOffset(2008, 8, 1, 0, 0, 0, _
                                New Timespan(6, 0, 0))
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date2))                                     
' Displays +6, +06, +06:00

回到表格Back to table

":" 自訂格式規範The ":" custom format specifier

":" 自訂格式規範表示時間分隔符號,用於區別時、分、秒。The ":" custom format specifier represents the time separator, which is used to differentiate hours, minutes, and seconds. 適當的當地語系化時間分隔符號是擷取自目前或指定之文化特性的 DateTimeFormatInfo.TimeSeparator 屬性。The appropriate localized time separator is retrieved from the DateTimeFormatInfo.TimeSeparator property of the current or specified culture.

注意

若要變更特定日期和時間字串的時間分隔符號,請在常值字串分隔符號內指定分隔字元。To change the time separator for a particular date and time string, specify the separator character within a literal string delimiter. 例如,自訂格式字串 hh'_'dd'_'ss 產生的結果字串中,一律使用 "_" (底線) 作為時間分隔符號。For example, the custom format string hh'_'dd'_'ss produces a result string in which "_" (an underscore) is always used as the time separator. 若要變更文化特性所有日期的時間分隔符號,請變更目前文化特性的 DateTimeFormatInfo.TimeSeparator 屬性值,或是具現化 DateTimeFormatInfo 物件、將字元指派給它的 TimeSeparator 屬性,並呼叫包含 IFormatProvider 參數的格式化方法之多載。To change the time separator for all dates for a culture, either change the value of the DateTimeFormatInfo.TimeSeparator property of the current culture, or instantiate a DateTimeFormatInfo object, assign the character to its TimeSeparator property, and call an overload of the formatting method that includes an IFormatProvider parameter.

如果單獨使用 ":" 格式規範,而沒有其他自訂格式規範,則會將它解譯為標準日期和時間格式規範,並擲回 FormatExceptionIf the ":" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

回到表格Back to table

"/" 自訂格式規範The "/" custom format specifier

"/" 自訂格式規範表示日期分隔符號,用於區別年、月、日。The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. 適當的當地語系化日期分隔符號是擷取自目前或指定之文化特性的 DateTimeFormatInfo.DateSeparator 屬性。The appropriate localized date separator is retrieved from the DateTimeFormatInfo.DateSeparator property of the current or specified culture.

注意

若要變更特定日期和時間字串的日期分隔符號,請在常值字串分隔符號內指定分隔字元。To change the date separator for a particular date and time string, specify the separator character within a literal string delimiter. 例如,自訂格式字串 mm'/'dd'/'yyyy 產生的結果字串中,一律使用 "/" 作為日期分隔符號。For example, the custom format string mm'/'dd'/'yyyy produces a result string in which "/" is always used as the date separator. 若要變更文化特性的所有日期之日期分隔符號,請變更目前文化特性的 DateTimeFormatInfo.DateSeparator 屬性值,或是具現化 DateTimeFormatInfo 物件、將字元指派給它的 DateSeparator 屬性,並呼叫包含 IFormatProvider 參數的格式化方法之多載。To change the date separator for all dates for a culture, either change the value of the DateTimeFormatInfo.DateSeparator property of the current culture, or instantiate a DateTimeFormatInfo object, assign the character to its DateSeparator property, and call an overload of the formatting method that includes an IFormatProvider parameter.

如果單獨使用 "/" 格式規範,而沒有其他自訂格式規範,則會將它解譯為標準日期和時間格式規範,並擲回 FormatExceptionIf the "/" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. 如需使用單一格式規範的詳細資訊,請參閱本主題稍後的使用單一自訂格式規範For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

回到表格Back to table

字元常值Character literals

自訂日期和時間格式字串中的下列字元是保留字元,一律會解譯為格式字元;若是 "、'、/ 和 \,則解譯為特殊字元。The following characters in a custom date and time format string are reserved and are always interpreted as formatting characters or, in the case of ", ', /, and \, as special characters.

FF HH KK MM dd
ff Gg hh mm s
tt yy zz % ::
/ "" '' |

所有其他字元一律會解譯為字元常值,並在格式化作業中,原封不動地包含在結果字串中。All other characters are always interpreted as character literals and, in a formatting operation, are included in the result string unchanged. 在剖析作業中,它們必須完全符合輸入字串中的字元;這項比較會區分大小寫。In a parsing operation, they must match the characters in the input string exactly; the comparison is case-sensitive.

下列範例包含常值字元 "PST" (太平洋標準時間) 和 "PDT" (太平洋日光節約時間),以表示格式字串中的當地時區。The following example includes the literal characters "PST" (for Pacific Standard Time) and "PDT" (for Pacific Daylight Time) to represent the local time zone in a format string. 請注意,此字串會包含在結果字串中,而且包含當地時區字串的字串也會成功剖析。Note that the string is included in the result string, and that a string that includes the local time zone string also parses successfully.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      String[] formats = { "dd MMM yyyy hh:mm tt PST", 
                           "dd MMM yyyy hh:mm tt PDT" };
      var dat = new DateTime(2016, 8, 18, 16, 50, 0);
      // Display the result string. 
      Console.WriteLine(dat.ToString(formats[1]));
      
      // Parse a string. 
      String value = "25 Dec 2016 12:00 pm PST";
      DateTime newDate;
      if (DateTime.TryParseExact(value, formats, null, 
                                 DateTimeStyles.None, out newDate)) 
         Console.WriteLine(newDate);
      else
         Console.WriteLine("Unable to parse '{0}'", value);
   }
}
// The example displays the following output:
//       18 Aug 2016 04:50 PM PDT
//       12/25/2016 12:00:00 PM
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim formats() As String = { "dd MMM yyyy hh:mm tt PST", 
                                  "dd MMM yyyy hh:mm tt PDT" }
      Dim dat As New Date(2016, 8, 18, 16, 50, 0)
      ' Display the result string. 
      Console.WriteLine(dat.ToString(formats(1)))
      
      ' Parse a string. 
      Dim value As String = "25 Dec 2016 12:00 pm PST"
      Dim newDate As Date
      If Date.TryParseExact(value, formats, Nothing, 
                            DateTimeStyles.None, newDate) Then 
         Console.WriteLine(newDate)
      Else
         Console.WriteLine("Unable to parse '{0}'", value)
      End If                               
   End Sub
End Module
' The example displays the following output:
'       18 Aug 2016 04:50 PM PDT
'       12/25/2016 12:00:00 PM

您可以使用兩種方式來指定將字元解譯為常值字元,而不是保留字元,以便包含在結果字串中,或在輸入字串中成功剖析:There are two ways to indicate that characters are to be interpreted as literal characters and not as reserve characters, so that they can be included in a result string or successfully parsed in an input string:

  • 將每個保留字元逸出。By escaping each reserved character. 如需詳細資訊,請參閱使用逸出字元For more information, see Using the Escape Character.

    下列範例包含常值字元 "pst" (太平洋標準時間),以表示格式字串中的當地時區。The following example includes the literal characters "pst" (for Pacific Standard time) to represent the local time zone in a format string. 由於 "s" 與 "t" 是自訂格式字串,因此必須逸出這兩個字元才能解譯為字元常值。Because both "s" and "t" are custom format strings, both characters must be escaped to be interpreted as character literals.

    using System;
    using System.Globalization;
    
    public class Example
    {
       public static void Main()
       {
          String format = "dd MMM yyyy hh:mm tt p\\s\\t";
          var dat = new DateTime(2016, 8, 18, 16, 50, 0);
          // Display the result string. 
          Console.WriteLine(dat.ToString(format));
          
          // Parse a string. 
          String value = "25 Dec 2016 12:00 pm pst";
          DateTime newDate;
          if (DateTime.TryParseExact(value, format, null, 
                                     DateTimeStyles.None, out newDate)) 
             Console.WriteLine(newDate);
          else
             Console.WriteLine("Unable to parse '{0}'", value);
       }
    }
    // The example displays the following output:
    //       18 Aug 2016 04:50 PM PDT
    //       12/25/2016 12:00:00 PM
    
    Imports System.Globalization
    
    Module Example
       Public Sub Main()
          Dim fmt As String = "dd MMM yyyy hh:mm tt p\s\t" 
          Dim dat As New Date(2016, 8, 18, 16, 50, 0)
          ' Display the result string. 
          Console.WriteLine(dat.ToString(fmt))
          
          ' Parse a string. 
          Dim value As String = "25 Dec 2016 12:00 pm pst"
          Dim newDate As Date
          If Date.TryParseExact(value, fmt, Nothing, 
                                DateTimeStyles.None, newDate) Then 
             Console.WriteLine(newDate)
          Else
             Console.WriteLine("Unable to parse '{0}'", value)
          End If                               
       End Sub
    End Module
    ' The example displays the following output:
    '       18 Aug 2016 04:50 PM pst
    '       12/25/2016 12:00:00 PM
    
  • 以雙引號或單引號括住整個常值字串。By enclosing the entire literal string in quotation marks or apostrophes. 下列範例類似上一個範例,不同之處在於 "pst" 會以雙引號括住,以表示整個分隔的字串都應該解譯為字元常值。The following example is like the previous one, except that "pst" is enclosed in quotation marks to indicate that the entire delimited string should be interpreted as character literals.

    using System;
    using System.Globalization;
    
    public class Example
    {
       public static void Main()
       {
          String format = "dd MMM yyyy hh:mm tt \"pst\"";
          var dat = new DateTime(2016, 8, 18, 16, 50, 0);
          // Display the result string. 
          Console.WriteLine(dat.ToString(format));
          
          // Parse a string. 
          String value = "25 Dec 2016 12:00 pm pst";
          DateTime newDate;
          if (DateTime.TryParseExact(value, format, null, 
                                     DateTimeStyles.None, out newDate)) 
             Console.WriteLine(newDate);
          else
             Console.WriteLine("Unable to parse '{0}'", value);
       }
    }
    // The example displays the following output:
    //       18 Aug 2016 04:50 PM PDT
    //       12/25/2016 12:00:00 PM
    
    Imports System.Globalization
    
    Module Example
       Public Sub Main()
          Dim fmt As String = "dd MMM yyyy hh:mm tt ""pst"""  
          Dim dat As New Date(2016, 8, 18, 16, 50, 0)
          ' Display the result string. 
          Console.WriteLine(dat.ToString(fmt))
          
          ' Parse a string. 
          Dim value As String = "25 Dec 2016 12:00 pm pst"
          Dim newDate As Date
          If Date.TryParseExact(value, fmt, Nothing, 
                                DateTimeStyles.None, newDate) Then 
             Console.WriteLine(newDate)
          Else
             Console.WriteLine("Unable to parse '{0}'", value)
          End If                               
       End Sub
    End Module
    ' The example displays the following output:
    '       18 Aug 2016 04:50 PM pst
    '       12/25/2016 12:00:00 PM
    

注意Notes

使用單一自訂格式規範Using single custom format specifiers

自訂日期和時間格式字串是由兩個或多個字元所組成。A custom date and time format string consists of two or more characters. 日期和時間格式化方法會將任何單一字元字串解譯為標準日期和時間格式字串。Date and time formatting methods interpret any single-character string as a standard date and time format string. 如果這些方法無法將該字元辨認為有效的格式規範,則會擲回 FormatExceptionIf they do not recognize the character as a valid format specifier, they throw a FormatException. 例如,僅由規範 "h" 所組成的格式字串會解譯為標準日期和時間字串。For example, a format string that consists only of the specifier "h" is interpreted as a standard date and time format string. 不過,在這種特殊情形下會擲回例外狀況,因為並沒有 "h" 標準日期和時間格式規範。However, in this particular case, an exception is thrown because there is no "h" standard date and timeformat specifier.

若要使用任何自訂日期和時間格式規範做為格式字串中的唯一規範 (也就是單獨使用 "d"、"f"、"F"、"g"、"h"、"H"、"K"、"m"、"M"、"s"、"t"、"y"、"z"、":" 或 "/" 自訂格式規範),請在規範之前或之後加上一個空格,或在單一自訂日期和時間規範之前加上一個百分比 ("%") 格式規範。To use any of the custom date and time format specifiers as the only specifier in a format string (that is, to use the "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" custom format specifier by itself), include a space before or after the specifier, or include a percent ("%") format specifier before the single custom date and time specifier.

例如,"%h" 會解譯為自訂日期和時間格式字串,該字串會顯示由目前日期和時間值所表示的小時。For example, "%h" is interpreted as a custom date and time format string that displays the hour represented by the current date and time value. 您也可以使用 " h" 或 "h " 格式字串,然而這會在結果字串中的小時旁邊加上空格。You can also use the " h" or "h " format string, although this includes a space in the result string along with the hour. 下列範例示範這三個格式字串。The following example illustrates these three format strings.

DateTime dat1 = new DateTime(2009, 6, 15, 13, 45, 0);

Console.WriteLine("'{0:%h}'", dat1);
Console.WriteLine("'{0: h}'", dat1);
Console.WriteLine("'{0:h }'", dat1);
// The example displays the following output:
//       '1'
//       ' 1'
//       '1 '
Dim dat1 As Date = #6/15/2009 1:45PM#

Console.WriteLine("'{0:%h}'", dat1)
Console.WriteLine("'{0: h}'", dat1)
Console.WriteLine("'{0:h }'", dat1)
' The example displays the following output:
'       '1'
'       ' 1'
'       '1 '

使用逸出字元Using the Escape Character

格式字串中的 "d"、"f"、"F"、"g"、"h"、"H"、"K"、"m"、"M"、"s"、"t"、"y"、"z"、":" 或 "/" 字元會解譯為自訂格式規範,而不是常值字元。The "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" characters in a format string are interpreted as custom format specifiers rather than as literal characters. 若要避免將字元解譯為格式規範,您可以在前面加上反斜線 (\),這是逸出字元。To prevent a character from being interpreted as a format specifier, you can precede it with a backslash (\), which is the escape character. 逸出字元表示接下來的字元是字元常值,應該原封不動地放入結果字串中。The escape character signifies that the following character is a character literal that should be included in the result string unchanged.

若要在結果字串中加上反斜線,您必須再加上一個反斜線 (變成\\),才能將反斜線解譯為常值。To include a backslash in a result string, you must escape it with another backslash (\\).

注意

某些編譯器 (例如 C++ 和 C# 編譯器) 也可能會將單一反斜線字元解譯為逸出字元。Some compilers, such as the C++ and C# compilers, may also interpret a single backslash character as an escape character. 為了確保字串在格式化時能夠正確獲得解譯,您可以在 C# 中的字串前面加上逐字字串常值字元 (@ 字元),或在 C# 和 C++ 中的每個反斜線前面再加上一個反斜線字元。To ensure that a string is interpreted correctly when formatting, you can use the verbatim string literal character (the @ character) before the string in C#, or add another backslash character before each backslash in C# and C++. 下列 C# 範例示範這兩種做法。The following C# example illustrates both approaches.

下列範例會使用逸出字元,以避免格式化作業將 "h" 和 "m" 字元解譯為格式規範。The following example uses the escape character to prevent the formatting operation from interpreting the "h" and "m" characters as format specifiers.

DateTime date = new DateTime(2009, 06, 15, 13, 45, 30, 90);
string fmt1 = "h \\h m \\m";
string fmt2 = @"h \h m \m";

Console.WriteLine("{0} ({1}) -> {2}", date, fmt1, date.ToString(fmt1));
Console.WriteLine("{0} ({1}) -> {2}", date, fmt2, date.ToString(fmt2));
// The example displays the following output:
//       6/15/2009 1:45:30 PM (h \h m \m) -> 1 h 45 m
//       6/15/2009 1:45:30 PM (h \h m \m) -> 1 h 45 m      
Dim date1 As Date = #6/15/2009 13:45#
Dim fmt As String = "h \h m \m"

Console.WriteLine("{0} ({1}) -> {2}", date1, fmt, date1.ToString(fmt))
' The example displays the following output:
'       6/15/2009 1:45:00 PM (h \h m \m) -> 1 h 45 m      

控制台設定Control Panel settings

[控制台] 中的 [地區及語言選項] 設定會影響格式化作業 (其中包含許多自訂日期和時間格式規範) 所產生的結果字串。The Regional and Language Options settings in Control Panel influence the result string produced by a formatting operation that includes many of the custom date and time format specifiers. 這些設定是用來初始化與目前執行緒文化特性相關的 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, you should specify a CultureInfo object, which represents a culture, or a DateTimeFormatInfo object.

許多自訂日期和時間格式規範所產生的結果字串,也取決於目前 DateTimeFormatInfo 物件的屬性。The result string produced by many of the custom date and time format specifiers also depends on properties of the current DateTimeFormatInfo object. 您的應用程式可以變更對應的 DateTimeFormatInfo 屬性,藉此改變某些自訂日期和時間格式規範所產生的結果。Your application can change the result produced by some custom date and time format specifiers by changing the corresponding DateTimeFormatInfo property. 例如,"ddd" 格式規範會將 AbbreviatedDayNames 字串陣列中找到的縮寫星期幾名稱加入至結果字串。For example, the "ddd" format specifier adds an abbreviated weekday name found in the AbbreviatedDayNames string array to the result string. 同樣地,"MMMM" 格式規範會將 MonthNames 字串陣列中找到的完整月份名稱加到結果字串。Similarly, the "MMMM" format specifier adds a full month name found in the MonthNames string array to the result string.

請參閱See Also

System.DateTime
System.IFormatProvider
格式化類型Formatting Types
標準日期和時間格式字串Standard Date and Time Format Strings
範例:.NET Framework 4 格式化公用程式Sample: .NET Framework 4 Formatting Utility