自訂 DateTime 格式字串

自訂 DateTime 格式字串是由一或多個自訂 DateTime 格式規範所組成,並且該格式字串會定義由格式化作業所產生之 DateTime 物件的文字表示。請注意,任何不為標準 DateTime 格式字串的字串皆會被解譯為自訂 DateTime 格式字串。

自訂 DateTime 格式規範

下表說明自訂 DateTime 格式規範。如需每種格式規範所產生之輸出的範例,請參閱自訂 DateTime 格式字串輸出範例

格式規範 說明

d

以 1 到 31 的數字代表月份的日期,單一位數的日期會設定為沒有前置零的格式。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

dd

以 01 到 31 的數字代表月份的日期,單一位數的日期會設定為有前置零的格式。

ddd

表示一週中星期幾的縮寫名稱,如目前 System.Globalization.DateTimeFormatInfo.AbbreviatedDayNames 屬性內所定義。

dddd (加上任何數目的其他 "d" 規範)

表示一週中星期幾的完整名稱,如目前 System.Globalization.DateTimeFormatInfo.DayNames 屬性內所定義。

f

表示秒數中的最大有效位數。

請注意,如果單獨使用 "f" 格式規範,而沒有其他格式規範,則會將它解譯為 "f" 標準 DateTime 格式規範 (完整日期/時間模式)。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

當您搭配 ParseExactTryParseExact 方法使用這個格式規範時,您使用的 "f" 格式規範數目會指示要剖析之秒數的最大有效位數。

ff

表示秒數中的兩個最大有效位數。

fff

表示秒數中的三個最大有效位數。

ffff

表示秒數中的四個最大有效位數。

fffff

表示秒數中的五個最大有效位數。

ffffff

表示秒數中的六個最大有效位數。

fffffff

表示秒數中的七個最大有效位數。

F

表示秒數中的最大有效位數。如果此數為零,則不顯示任何數字。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

當您搭配 ParseExactTryParseExact 方法使用這個格式規範時,您使用的 "F" 格式規範數目會指示要剖析之秒數的最大有效位數上限。

FF

表示秒數中的兩個最大有效位數。不過,結尾的零或兩個零的數字都不會顯示。

FFF

表示秒數中的三個最大有效位數。不過,結尾的零或三個零的數字都不會顯示。

FFFF

表示秒數中的四個最大有效位數。不過,結尾的零或四個零的數字都不會顯示。

FFFFF

表示秒數中的五個最大有效位數。不過,結尾的零或五個零的數字都不會顯示。

FFFFFF

表示秒數中的六個最大有效位數。不過,結尾的零或六個零的數字都不會顯示。

FFFFFFF

表示秒數中的七個最大有效位數。不過,結尾的零或七個零的數字都不會顯示。

g 或 gg (加上任何數目的其他 "g" 規範)

表示時期或時代 (例如,西元)。如果要格式化的日期沒有關聯的時期或時代字串,則會忽略這個規範。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

h

以 1 到 12 的數字來表示小時,也就是以 12 小時制來表示小時,從午夜或中午開始計算整點時數。因此,午夜後的特定小時與下午的相同小時會難以區分。小時是不進位的,而且單一位數的小時會格式化為沒有前置零的數字。例如,假設時間為 5:43,則此格式規範會顯示 "5"。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

hh, hh (加上任何數目的其他 "h" 規範)

以 01 到 12 的數字來表示小時,也就是以 12 小時制來表示小時,從午夜或中午開始計算整點時數。因此,午夜後的特定小時與下午的相同小時會難以區分。小時是不進位的,而且單一位數的小時會格式化為有前置零的數字。例如,假設時間為 5:43,則此格式規範會顯示 "05"。

H

以 0 到 23 的數字來表示小時,也就是以 24 小時制來表示小時 (從零開始),從午夜開始計算時數。單一位數的小時會格式化為沒有前置零的數字。

HH, HH (加上任何數目的其他 "H" 規範)

以 00 到 23 的數字來表示小時,也就是以 24 小時制來表示小時 (從零開始),從午夜開始計算時數。單一位數的小時會格式化為具有前置零的數字。

K

表示 DateTime.Kind 屬性的不同值,即 Local、Utc 或 Unspecified。這個規範會反覆存取文字類的值,並保存時區。如果是 Local 類的值,此規範相當於 "zzz" 規範,並且會顯示當地時間的位移值,例如 "-07:00"。如果是 Utc 類的值,此規範會顯示一個 "Z" 字元來表示 UTC 日期。如果是 Unspecified 類的值,此規範相當於 "" (nothing)。

m

以 0 到 59 的數字來表示分鐘,分鐘表示過去一個小時內經過的完整分鐘數。單一位數的分鐘會格式化為沒有前置零的數字。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

mm, mm (加上任何數目的其他 "m" 規範)

以 00 到 59 的數字來表示分鐘,分鐘表示過去一個小時內經過的完整分鐘數。單一位數的分鐘會格式化為具有前置零的數字。

M

以 1 到 12 的數字代表月份,單一位數的月份會設定為沒有前置零的格式。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

MM

以 01 到 12 的數字代表月份,單一位數的月份會設定為具有前置零的格式。

MMM

表示月份的縮寫名稱,如目前 System.Globalization.DateTimeFormatInfo.AbbreviatedMonthNames 屬性內所定義。

MMMM

表示月份的完整名稱,如目前 System.Globalization.DateTimeFormatInfo.MonthNames 屬性內所定義。

s

以 0 到 59 的數字來表示秒數,秒數表示自從過去一分鐘之後所經過的完整秒數。單一位數的秒數會格式化為沒有前置零的數字。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

ss, ss (加上任何數目的其他 "s" 規範)

以 00 到 59 的數字來表示秒數,秒數表示過去一分鐘所經過的完整秒數。單一位數的秒數會格式化為具有前置零的數字。

t

表示 A.M./P.M. 指示項的第一個字元,如目前 System.Globalization.DateTimeFormatInfo.AMDesignatorSystem.Globalization.DateTimeFormatInfo.PMDesignator 屬性內所定義。如果時間的時數格式化為小於 12 的數字,則會使用 A.M. 指示項,否則會使用 P.M. 指示項。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

tt, tt (加上任何數目的其他 "t" 規範)

表示 A.M./P.M. 指示項,如目前 System.Globalization.DateTimeFormatInfo.AMDesignatorSystem.Globalization.DateTimeFormatInfo.PMDesignator 屬性內所定義。如果時間的時數格式化為小於 12 的數字,則會使用 A.M. 指示項,否則會使用 P.M. 指示項。

y

以最多兩個位數的數字來表示年份。如果年份超過兩個位數,結果中只會出現兩個低位數字。如果年份少於兩個位數,則會格式化為沒有前置零的數字。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

yy

以兩個位數的數字來表示年份。如果年份超過兩個位數,結果中只會出現兩個低位數字。如果年份少於兩個位數,則會以前置零填補此數字,以達到兩個位數。

yyy

以三個位數的數字來表示年份。如果年份超過三個位數,結果中只會出現三個低位數字。如果年份少於三個位數,則會以前置零填補此數字,以達到三個位數。

請注意,如果是可使用五個位數之年份的泰國佛教曆法,此格式規範便會顯示所有的五個位數。

yyyy

以四個位數的數字來表示年份。如果年份超過四個位數,結果中只會出現四個低位數字。如果年份少於四個位數,則會以前置零填補此數字,以達到四個位數。

請注意,如果是可使用五個位數之年份的泰國佛教曆法,此格式規範便會顯示所有的五個位數。

yyyyy (加上任何數目的其他 "y" 規範)

以五個位數的數字來表示年份。如果年份超過五個位數,結果中只會出現五個低位數字。如果年份少於五個位數,則會以前置零填補此數字,以達到五個位數。

如果有其他的 "y" 規範,則會視需要以前置零填補此數字,使其達到 "y" 規範的數目。

z

表示您的系統時區與格林威治標準時間 (GMT) 之間的時差 (帶正負號),以小時為測量單位。例如,採用太平洋標準時間的電腦,其時差為 "-8"。

顯示時差時,一定會有前置正負號。加號 (+) 表示 GMT 之前的時數,而減號 (-) 則表示 GMT 之後的時數。時差範圍從 –12 到 +13,單一位數的時差會格式化為沒有前置零的數字。時差會受日光節約時間影響。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

zz

表示您的系統時區與格林威治標準時間 (GMT) 之間的時差 (帶正負號),以小時為測量單位。例如,採用太平洋標準時間的電腦,其時差為 "-08"。

顯示時差時,一定會有前置正負號。加號 (+) 表示 GMT 之前的時數,而減號 (-) 則表示 GMT 之後的時數。時差範圍從 –12 到 +13,單一位數的時差會格式化為具有前置零的數字。時差會受日光節約時間影響。

zzz, zzz (加上任何數目的其他 "z" 規範)

表示您的系統時區與格林威治標準時間 (GMT) 之間的時差 (帶正負號),以小時和分鐘為測量單位。例如,採用太平洋標準時間的電腦,其時差為 "-08:00"。

顯示時差時,一定會有前置正負號。加號 (+) 表示 GMT 之前的時數,而減號 (-) 則表示 GMT 之後的時數。時差範圍從 –12 到 +13,單一位數的時差會格式化為具有前置零的數字。時差會受日光節約時間影響。

:

定義在目前 System.Globalization.DateTimeFormatInfo.TimeSeparator 屬性中的時間分隔符號,用於區別小時、分鐘和秒數。

/

定義在目前 System.Globalization.DateTimeFormatInfo.DateSeparator 屬性中的日期分隔符號,用於區別年、月和日。

"

引號括住的字串 (引號)。顯示在兩個引號 (") 之間的任何字串的常值,每個引號前面會搭配一個逸出字元 (Escape Character) (\)。

'

括住的字串 (縮寫符號)。顯示在兩個縮寫符號 (') 字元之間的任何字串的常值。

%c

表示與自訂格式規範 "c" 相關的結果,但前提是自訂 DateTime 格式字串僅由該自訂格式規範所組成。也就是說,若要使用 "d"、"f"、"F"、"h"、"m"、"s"、"t"、"y"、"z"、"H" 或 "M" 自訂格式規範本身,則可以指定 "%d"、"%f"、"%F"、"%h"、"%m"、"%s"、"%t"、"%y"、"%z"、"%H" 或 "%M"。如需使用單一格式規範的詳細資訊,請參閱使用單一自訂格式規範。

\c

逸出字元。當字元 "c" 的前面有逸出字元 (\) 時,將它顯示為常值 (Literal)。若要在結果字串內插入反斜線字元本身,請使用兩個逸出字元 ("\\")。

任意字元

其他任何字元都會複製到結果字串,並且不影響格式。

控制台設定值

[控制台] 中 [地區及語言選項] 項目的設定會影響格式化作業所產生的結果字串。這些設定是用來初始化與目前執行緒文化特性相關聯的 DateTimeFormatInfo 物件,而且目前的執行緒文化特性會提供用來管理格式的值。使用不同設定的電腦將會產生不同的結果字串。

DateTimeFormatInfo 屬性

格式會受到目前 DateTimeFormatInfo 物件的影響,而此物件是由目前執行緒文化特性隱含提供或由叫用格式之方法的 IFormatProvider 參數明確提供。針對 IFormatProvider 參數指定代表文化特性的 CultureInfo 物件或是指定 DateTimeFormatInfo 物件。

許多標準 DateTime 格式規範為格式化模式的別名,這些模式是由目前 DateTimeFormatInfo 物件的屬性所定義。這表示您可以變更對應的 DateTimeFormatInfo 屬性,藉此來改變由某些標準 DateTime 格式規範所產生的結果。

使用單一自訂格式規範

自訂 DateTime 格式字串是由兩個或多個字元所組成。例如,如果格式字串僅由規範 "h" 所組成,則此格式字串會解譯為標準 DateTime 格式字串。不過,在這種特定情形下會擲回例外狀況,因為並沒有 "h" 標準 DateTime 格式規範。

若要使用單一自訂 DateTime 格式規範,請在 DateTime 規範之前或之後包含一個空格,或在 DateTime 規範之前包含一個百分比 (%) 格式規範。例如,格式字串 "h" 和 "%h" 會解譯為自訂 DateTime 格式字串,該字串會顯示由目前 DateTime 物件所代表的小時。請注意,如果使用了空格,它在結果字串中會以常值字元的形式出現。

使用自訂格式字串

下列程式碼片段會說明如何從 DateTime 物件建立自訂格式字串。這個範例假設目前的文化特性為英文-美國 (en-US)。

Dim MyDate As New DateTime(2000, 1, 1, 0, 0, 0)
Dim MyString As String = MyDate.ToString("dddd - d - MMMM")
' In the U.S. English culture, MyString has the value: 
' "Saturday - 1 - January".
MyString = MyDate.ToString("yyyy gg")
' In the U.S. English culture, MyString has the value: "2000 A.D.".
DateTime MyDate = new DateTime(2000, 1, 1, 0, 0, 0);
String MyString = MyDate.ToString("dddd - d - MMMM");
// In the U.S. English culture, MyString has the value: 
// "Saturday - 1 - January".
MyString = MyDate.ToString("yyyy gg");
// In the U.S. English culture, MyString has the value: "2000 A.D.".

範例

下列程式碼範例會示範由每個自訂 DateTime 格式規範所產生的結果。

' This code example demonstrates the result yielded by each custom 
' DateTime format specifier.
Imports System
Imports System.Globalization

Class Sample
    Public Shared Sub Main() 
        Dim formatString As String
        Dim ci As New CultureInfo("")
        Dim msgDTctor As String = "The DateTime constructor for " & _
                                  "January 3, 0001 A.D., 02:09:20.444 P.M. is" & vbLf & _
                                  "    DateTime(0001, 1, 3, 14, 9, 20, 444)" & vbLf
        Dim  thisDate As New DateTime(1, 1, 3, 14, 9, 20, 444)
        Dim   utcDate As New DateTime(0002, 2, 4, 15, 10, 21, 555, DateTimeKind.Utc)
        Dim localDate As New DateTime(0003, 3, 5, 16, 11, 22, 666, DateTimeKind.Local)
        
        ' Custom DateTime format specifiers.
        ' The specifiers are for year, month, day, era, hour, minute, second, fraction of a second, 
        ' A.M./P.M. designator, UTC offset, quoted string (apostrophe), quoted string (quotation mark),
        ' and escape character.
        Dim specifiers As String() =  New String() { _
                                      "%y", "yy", "yyy", "yyyy", "yyyyy", _
                                       "%M", "MM", "MMM", "MMMM", _
                                       "%d", "dd", "ddd", "dddd", _
                                       "%g", "gg", _
                                       "%h", "hh", "%H", "HH", _
                                       "%m", "mm", _
                                       "%s", "ss", _
                                       "%f", "ff", "fff", "ffff", "fffff", "ffffff", "fffffff", _
                                       "%F", "FF", "FFF", "FFFF", "FFFFF", "FFFFFF", "FFFFFFF", _
                                       "%t", "tt", _
                                       "%z", "zz", "zzz", _
                                       "'The year is' yyyy", _
                                       """The year is"" yyyy", _
                                       "\\"}

        Console.Clear()
        Console.WriteLine("The culture is {0}.", ci.DisplayName)
        Console.WriteLine(msgDTctor)
        Console.WriteLine("{0,20} {1}", "Custom format string", "Yields")
        Console.WriteLine("{0,20} {1}", "--------------------", "------")
        Dim format As String
        For Each format In specifiers
            formatString = """" & format & """"
            Console.WriteLine("{0,20} ""{1}""", formatString, thisDate.ToString(format))
        Next format

     Console.WriteLine()
     Console.WriteLine()

' Example of using "K" format specifier.
     Console.WriteLine("Format different kinds of DateTime using ""K""")
     Console.WriteLine("{0, 20} {1}", "DateTimeKind", "Yields")
     Console.WriteLine("{0,20} {1}", "------------", "------")
     Console.WriteLine("{0,20} ""{1}""",  thisDate.Kind,  thisDate.ToString("%K"))
     Console.WriteLine("{0,20} ""{1}""",   utcDate.Kind,   utcDate.ToString("%K"))
     Console.WriteLine("{0,20} ""{1}""", localDate.Kind, localDate.ToString("%K"))
        
    End Sub 'Main
End Class 'Sample

'This code example produces the following results:
'
'The culture is Invariant Language (Invariant Country).
'The DateTime constructor for January 3, 0001 A.D., 02:09:20.444 P.M. is
'    DateTime(0001, 1, 3, 14, 9, 20, 444)
'
'Custom format string Yields
'-------------------- ------
'                "%y" "1"
'                "yy" "01"
'               "yyy" "001"
'              "yyyy" "0001"
'             "yyyyy" "00001"
'                "%M" "1"
'                "MM" "01"
'               "MMM" "Jan"
'              "MMMM" "January"
'                "%d" "3"
'                "dd" "03"
'               "ddd" "Wed"
'              "dddd" "Wednesday"
'                "%g" "A.D."
'                "gg" "A.D."
'                "%h" "2"
'                "hh" "02"
'                "%H" "14"
'                "HH" "14"
'                "%m" "9"
'                "mm" "09"
'                "%s" "20"
'                "ss" "20"
'                "%f" "4"
'                "ff" "44"
'               "fff" "444"
'              "ffff" "4440"
'             "fffff" "44400"
'            "ffffff" "444000"
'           "fffffff" "4440000"
'                "%F" "4"
'                "FF" "44"
'               "FFF" "444"
'              "FFFF" "444"
'             "FFFFF" "444"
'            "FFFFFF" "444"
'           "FFFFFFF" "444"
'                "%t" "P"
'                "tt" "PM"
'                "%z" "-8"
'                "zz" "-08"
'               "zzz" "-08:00"
'"'The year is' yyyy" "The year is 0001"
'""The year is" yyyy" "The year is 0001"
'                "\\" "\"
'
'
'Format different kinds of DateTime using "K"
'        DateTimeKind Yields
'        ------------ ------
'         Unspecified ""
'                 Utc "Z"
'               Local "-08:00"
// This code example demonstrates the result yielded by each custom 
// DateTime format specifier.

using System;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
    string formatString;
    CultureInfo ci = new CultureInfo("");
    string msgDTctor = "The DateTime constructor for " + 
                       "January 3, 0001 A.D., 02:09:20.444 P.M. is\n" +
                       "    DateTime(0001, 1, 3, 14, 9, 20, 444)\n";
    DateTime thisDate  = new DateTime(0001, 1, 3, 14, 9, 20, 444);  // This is equal to DateTime thisDate  = new DateTime(0001, 1, 3, 14, 9, 20, 444, DateTime.Unspecified)
    DateTime utcDate   = new DateTime(0002, 2, 4, 15, 10, 21, 555, DateTimeKind.Utc);
    DateTime localDate = new DateTime(0003, 3, 5, 16, 11, 22, 666, DateTimeKind.Local);

// Custom DateTime format specifiers
    string[] specifiers = {
// Year
        "%y", 
        "yy", 
        "yyy", 
        "yyyy", 
        "yyyyy", 
// Month
        "%M", 
        "MM", 
        "MMM", 
        "MMMM", 
// Day
        "%d", 
        "dd", 
        "ddd", 
        "dddd",
// Era
        "%g",  
        "gg",  
// Hour
        "%h", 
        "hh", 
        "%H", 
        "HH", 
// Minute
        "%m", 
        "mm", 
// Second
        "%s", 
        "ss", 
// Fraction of a second
        "%f", 
        "ff", 
        "fff", 
        "ffff", 
        "fffff", 
        "ffffff", 
        "fffffff", 
        "%F", 
        "FF", 
        "FFF", 
        "FFFF", 
        "FFFFF", 
        "FFFFFF", 
        "FFFFFFF", 
// A.M./P.M. designator
        "%t", 
        "tt", 
// UTC offset
        "%z", 
        "zz", 
        "zzz", 
// Quoted string (apostrophe)
        "'The year is' yyyy", 
// Quoted string (quotation mark)
        "\"The year is\" yyyy",
// Escape character
        @"\\", 
        };
//
     Console.Clear();
     Console.WriteLine("The culture is {0}.", ci.DisplayName);
     Console.WriteLine(msgDTctor);
     Console.WriteLine("{0,20} {1}", "Custom format string", "Yields");
     Console.WriteLine("{0,20} {1}", "--------------------", "------");
     foreach (string format in specifiers)
         {
         formatString = "\"" + format + "\"";
         Console.WriteLine("{0,20} \"{1}\"", 
                            formatString, thisDate.ToString(format));
         }

     Console.WriteLine();
     Console.WriteLine();

// Example of using "K" format specifier.
     Console.WriteLine("Format different kinds of DateTime using \"K\"");
     Console.WriteLine("{0, 20} {1}", "DateTimeKind", "Yields");
     Console.WriteLine("{0,20} {1}", "------------", "------");
     Console.WriteLine("{0,20} \"{1}\"",  thisDate.Kind,  thisDate.ToString("%K"));
     Console.WriteLine("{0,20} \"{1}\"",   utcDate.Kind,   utcDate.ToString("%K"));
     Console.WriteLine("{0,20} \"{1}\"", localDate.Kind, localDate.ToString("%K"));

    }
}

/*
This code example produces the following results:

The culture is Invariant Language (Invariant Country).
The DateTime constructor for January 3, 0001 A.D., 02:09:20.444 P.M. is
    DateTime(0001, 1, 3, 14, 9, 20, 444)

Custom format string Yields
-------------------- ------
                "%y" "1"
                "yy" "01"
               "yyy" "001"
              "yyyy" "0001"
             "yyyyy" "00001"
                "%M" "1"
                "MM" "01"
               "MMM" "Jan"
              "MMMM" "January"
                "%d" "3"
                "dd" "03"
               "ddd" "Wed"
              "dddd" "Wednesday"
                "%g" "A.D."
                "gg" "A.D."
                "%h" "2"
                "hh" "02"
                "%H" "14"
                "HH" "14"
                "%m" "9"
                "mm" "09"
                "%s" "20"
                "ss" "20"
                "%f" "4"
                "ff" "44"
               "fff" "444"
              "ffff" "4440"
             "fffff" "44400"
            "ffffff" "444000"
           "fffffff" "4440000"
                "%F" "4"
                "FF" "44"
               "FFF" "444"
              "FFFF" "444"
             "FFFFF" "444"
            "FFFFFF" "444"
           "FFFFFFF" "444"
                "%t" "P"
                "tt" "PM"
                "%z" "-8"
                "zz" "-08"
               "zzz" "-08:00"
"'The year is' yyyy" "The year is 0001"
""The year is" yyyy" "The year is 0001"
                "\\" "\"

Format different kinds of DateTime using "K"
        DateTimeKind Yields
        ------------ ------
         Unspecified ""
                 Utc "Z"
               Local "-08:00"                
*/
// This code example demonstrates the result yielded by each custom 
// DateTime format specifier.

using namespace System;
using namespace System::Globalization;

    int main() 
    {
    String^ formatString;
    CultureInfo^ ci = gcnew CultureInfo("");
    String^ msgDTctor = "The DateTime constructor for "  
                       "January 3, 0001 A.D., 02:09:20.444 P.M. is\n" 
                       "    DateTime(0001, 1, 3, 14, 9, 20, 444)\n";
    DateTime^  thisDate = gcnew DateTime(0001, 1, 3, 14, 9, 20, 444);
    DateTime^   utcDate = gcnew DateTime(0002, 2, 4, 15, 10, 21, 555, DateTimeKind::Utc);
    DateTime^ localDate = gcnew DateTime(0003, 3, 5, 16, 11, 22, 666, DateTimeKind::Local);

// Custom DateTime format specifiers
    array<String^>^ specifiers = gcnew array<String^> {
// Year
        "%y", 
        "yy", 
        "yyy", 
        "yyyy", 
        "yyyyy", 
// Month
        "%M", 
        "MM", 
        "MMM", 
        "MMMM", 
// Day
        "%d", 
        "dd", 
        "ddd", 
        "dddd",
// Era
        "%g",  
        "gg",  
// Hour
        "%h", 
        "hh", 
        "%H", 
        "HH", 
// Minute
        "%m", 
        "mm", 
// Second
        "%s", 
        "ss", 
// Fraction of a second
        "%f", 
        "ff", 
        "fff", 
        "ffff", 
        "fffff", 
        "ffffff", 
        "fffffff", 
        "%F", 
        "FF", 
        "FFF", 
        "FFFF", 
        "FFFFF", 
        "FFFFFF", 
        "FFFFFFF", 
// A.M./P.M. designator
        "%t", 
        "tt", 
// UTC offset
        "%z", 
        "zz", 
        "zzz", 
// Quoted string (apostrophe)
        "'The year is' yyyy", 
// Quoted string (quotation mark)
        "\"The year is\" yyyy",
// Escape character
        "\\\\", 
        };
//
     Console::Clear();
     Console::WriteLine("The culture is {0}.", ci->DisplayName);
     Console::WriteLine(msgDTctor);
     Console::WriteLine("{0,20} {1}", "Custom format string", "Yields");
     Console::WriteLine("{0,20} {1}", "--------------------", "------");
     for each (String^ format in specifiers)
         {
         formatString = "\"" + format + "\"";
         Console::WriteLine("{0,20} \"{1}\"", 
                            formatString, thisDate->ToString(format));
         }

     Console::WriteLine();
     Console::WriteLine();

// Example of using "K" format specifier.
     Console::WriteLine("Format different kinds of DateTime using \"K\"");
     Console::WriteLine("{0, 20} {1}", "DateTimeKind", "Yields");
     Console::WriteLine("{0,20} {1}", "------------", "------");
     Console::WriteLine("{0,20} \"{1}\"",  thisDate->Kind,  thisDate->ToString("%K"));
     Console::WriteLine("{0,20} \"{1}\"",   utcDate->Kind,   utcDate->ToString("%K"));
     Console::WriteLine("{0,20} \"{1}\"", localDate->Kind, localDate->ToString("%K"));
     
    }



/*
This code example produces the following results:

The culture is Invariant Language (Invariant Country).
The DateTime constructor for January 3, 0001 A.D., 02:09:20.444 P.M. is
    DateTime(0001, 1, 3, 14, 9, 20, 444)

Custom format string Yields
-------------------- ------
                "%y" "1"
                "yy" "01"
               "yyy" "001"
              "yyyy" "0001"
             "yyyyy" "00001"
                "%M" "1"
                "MM" "01"
               "MMM" "Jan"
              "MMMM" "January"
                "%d" "3"
                "dd" "03"
               "ddd" "Wed"
              "dddd" "Wednesday"
                "%g" "A.D."
                "gg" "A.D."
                "%h" "2"
                "hh" "02"
                "%H" "14"
                "HH" "14"
                "%m" "9"
                "mm" "09"
                "%s" "20"
                "ss" "20"
                "%f" "4"
                "ff" "44"
               "fff" "444"
              "ffff" "4440"
             "fffff" "44400"
            "ffffff" "444000"
           "fffffff" "4440000"
                "%F" "4"
                "FF" "44"
               "FFF" "444"
              "FFFF" "444"
             "FFFFF" "444"
            "FFFFFF" "444"
           "FFFFFFF" "444"
                "%t" "P"
                "tt" "PM"
                "%z" "-8"
                "zz" "-08"
               "zzz" "-08:00"
"'The year is' yyyy" "The year is 0001"
""The year is" yyyy" "The year is 0001"
                "\\" "\"


Format different kinds of DateTime using "K"
        DateTimeKind Yields
        ------------ ------
         Unspecified ""
                 Utc "Z"
               Local "-08:00"
*/

請參閱

參考

System.DateTime
System.IFormatProvider

概念

自訂 DateTime 格式字串輸出範例
標準 DateTime 格式字串

其他資源

格式化型別