自定义日期和时间格式字符串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. 默认情况下,结果字符串反映 zh-cn 区域性的格式设置约定。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" 月份(1 到 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.

当使用作为格式字符串的一部分提供给 ParseExactTryParseExactParseExactTryParseExact 方法的“f”格式说明符时,所用的“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.

下面的示例显示对美国太平洋时区中的系统上的各种 DateTimeDateTimeOffset 值使用“K”自定义格式说明符。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. 下面的示例通过使用默认 zh-cn 区域性(在这种情况下为当前区域性)的公历来分析具有二位数年份的日期的字符串表示形式。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