DateTimeFormatInfo 類別

本文提供此 API 參考文件的補充備註。

類別的屬性 DateTimeFormatInfo 包含格式化或剖析日期和時間值的文化特性特定資訊,例如:

  • 用來格式化日期值的模式。
  • 用來格式化時間值的模式。
  • 星期幾的名稱。
  • 年度月份的名稱。
  • 時間值中使用的 A.M. 和 P.M. 指定項。
  • 表示日期的行事曆。

具現化 DateTimeFormatInfo 物件

DateTimeFormatInfo物件可以代表不因文化特性、特定文化特性、中性文化特性或目前文化特性的格式化慣例。 本節討論如何具現化每種物件類型 DateTimeFormatInfo

具現化非變異文化特性的 DateTimeFormatInfo 物件

不因文化特性而異的文化特性代表不區分文化特性的文化特性。 它是以英文為基礎,但不是以任何特定的英文國家/地區為基礎。 雖然特定文化特性的數據可以是動態的,而且可以變更以反映新的文化慣例或使用者喜好設定,但非變異文化特性的數據不會變更。 您可以具現化 DateTimeFormatInfo 物件,此物件代表不因文化特性而異的格式慣例,其方式如下:

下列範例會使用上述每一 DateTimeFormatInfo 種方法來具現化代表不因文化特性而異的物件。 然後,它會指出物件是否為唯讀。

System.Globalization.DateTimeFormatInfo dtfi;

dtfi = System.Globalization.DateTimeFormatInfo.InvariantInfo;
Console.WriteLine(dtfi.IsReadOnly);

dtfi = new System.Globalization.DateTimeFormatInfo();
Console.WriteLine(dtfi.IsReadOnly);

dtfi = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat;
Console.WriteLine(dtfi.IsReadOnly);
// The example displays the following output:
//       True
//       False
//       True

具現化特定文化特性的 DateTimeFormatInfo 物件

特定文化特性代表在特定國家/地區中說的語言。 例如,en-US 是代表 美國 中說英語的特定文化特性,en-CA 是代表加拿大口語的特定文化特性。 您可以具現化 DateTimeFormatInfo 物件,以下列方式呈現特定文化特性的格式設定慣例:

下列範例說明將物件具現化 DateTimeFormatInfo 的每個方式,並指出產生的物件是否為唯讀。

System.Globalization.CultureInfo ci = null;
System.Globalization.DateTimeFormatInfo dtfi = null;

// Instantiate a culture using CreateSpecificCulture.
ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
dtfi = ci.DateTimeFormat;
Console.WriteLine("{0} from CreateSpecificCulture: {1}", ci.Name, dtfi.IsReadOnly);

// Instantiate a culture using the CultureInfo constructor.
ci = new System.Globalization.CultureInfo("en-CA");
dtfi = ci.DateTimeFormat;
Console.WriteLine("{0} from CultureInfo constructor: {1}", ci.Name, dtfi.IsReadOnly);

// Retrieve a culture by calling the GetCultureInfo method.
ci = System.Globalization.CultureInfo.GetCultureInfo("en-AU");
dtfi = ci.DateTimeFormat;
Console.WriteLine("{0} from GetCultureInfo: {1}", ci.Name, dtfi.IsReadOnly);

// Instantiate a DateTimeFormatInfo object by calling DateTimeFormatInfo.GetInstance.
ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
dtfi = System.Globalization.DateTimeFormatInfo.GetInstance(ci);
Console.WriteLine("{0} from GetInstance: {1}", ci.Name, dtfi.IsReadOnly);

// The example displays the following output:
//      en-US from CreateSpecificCulture: False
//      en-CA from CultureInfo constructor: False
//      en-AU from GetCultureInfo: True
//      en-GB from GetInstance: False

具現化中性文化特性的 DateTimeFormatInfo 物件

中性文化代表與國家/地區無關的文化特性或語言;它通常是一或多個特定文化特性的父系。 例如,Fr 是法文語言的中性文化特性,也是fr-FR文化特性的父系。 您可以具現化 DateTimeFormatInfo 物件,該物件代表中性文化特性的格式慣例,其方式與您建立 DateTimeFormatInfo 代表特定文化特性之格式化慣例的物件相同。 此外,您可以從特定文化特性的 屬性擷取中性文化特性,並擷DateTimeFormatInfo取其 CultureInfo.DateTimeFormat 屬性傳回的物件,以擷取中性文化DateTimeFormatInfoCultureInfo.Parent特性的物件。 除非父文化特性代表不因文化特性而異,否則傳 DateTimeFormatInfo 回的物件是可擦寫的。 下列範例說明如何具現 DateTimeFormatInfo 化代表中性文化特性的物件。

System.Globalization.CultureInfo specific, neutral;
System.Globalization.DateTimeFormatInfo dtfi;

// Instantiate a culture by creating a specific culture and using its Parent property.
specific = System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
neutral = specific.Parent;
dtfi = neutral.DateTimeFormat;
Console.WriteLine("{0} from Parent property: {1}", neutral.Name, dtfi.IsReadOnly);

dtfi = System.Globalization.CultureInfo.GetCultureInfo("fr-FR").Parent.DateTimeFormat;
Console.WriteLine("{0} from Parent property: {1}", neutral.Name, dtfi.IsReadOnly);

// Instantiate a neutral culture using the CultureInfo constructor.
neutral = new System.Globalization.CultureInfo("fr");
dtfi = neutral.DateTimeFormat;
Console.WriteLine("{0} from CultureInfo constructor: {1}", neutral.Name, dtfi.IsReadOnly);

// Instantiate a culture using CreateSpecificCulture.
neutral = System.Globalization.CultureInfo.CreateSpecificCulture("fr");
dtfi = neutral.DateTimeFormat;
Console.WriteLine("{0} from CreateSpecificCulture: {1}", neutral.Name, dtfi.IsReadOnly);

// Retrieve a culture by calling the GetCultureInfo method.
neutral = System.Globalization.CultureInfo.GetCultureInfo("fr");
dtfi = neutral.DateTimeFormat;
Console.WriteLine("{0} from GetCultureInfo: {1}", neutral.Name, dtfi.IsReadOnly);

// Instantiate a DateTimeFormatInfo object by calling GetInstance.
neutral = System.Globalization.CultureInfo.CreateSpecificCulture("fr");
dtfi = System.Globalization.DateTimeFormatInfo.GetInstance(neutral);
Console.WriteLine("{0} from GetInstance: {1}", neutral.Name, dtfi.IsReadOnly);

// The example displays the following output:
//       fr from Parent property: False
//       fr from Parent property: False
//       fr from CultureInfo constructor: False
//       fr-FR from CreateSpecificCulture: False
//       fr from GetCultureInfo: True
//       fr-FR from GetInstance: False

不過,中性文化缺乏特定文化特性的格式資訊,因為它與特定國家/地區無關。 .NET 會傳回DateTimeFormatInfo物件,以反映屬於中性文化特性子系之特定文化特性的格式慣例,而不是以泛型值填DateTimeFormatInfo入物件。 例如, DateTimeFormatInfo 中性 en 文化特性的物件會反映 en-US 文化特性的格式設定慣例, DateTimeFormatInfo 而 fr 文化特性的物件則反映 fr-FR 文化特性的格式設定慣例。

您可以使用類似下列的程式代碼來判斷中性文化特性所代表的特定文化特性格式慣例。 此範例會使用反映來比較 DateTimeFormatInfo 中性文化特性的屬性與特定子文化特性的屬性。 如果兩個行事曆是相同的行事曆類型,而且如果 GregorianCalendar.CalendarType 公曆的屬性具有相同值,它會將兩個行事曆視為相等。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;

public class InstantiateEx6
{
    public static void Main()
    {
        // Get all the neutral cultures
        List<String> names = new List<String>();
        Array.ForEach(CultureInfo.GetCultures(CultureTypes.NeutralCultures),
                      culture => names.Add(culture.Name));
        names.Sort();
        foreach (var name in names)
        {
            // Ignore the invariant culture.
            if (name == "") continue;

            ListSimilarChildCultures(name);
        }
    }

    private static void ListSimilarChildCultures(String name)
    {
        // Create the neutral DateTimeFormatInfo object.
        DateTimeFormatInfo dtfi = CultureInfo.GetCultureInfo(name).DateTimeFormat;
        // Retrieve all specific cultures of the neutral culture.
        CultureInfo[] cultures = Array.FindAll(CultureInfo.GetCultures(CultureTypes.SpecificCultures),
                                 culture => culture.Name.StartsWith(name + "-", StringComparison.OrdinalIgnoreCase));
        // Create an array of DateTimeFormatInfo properties
        PropertyInfo[] properties = typeof(DateTimeFormatInfo).GetProperties(BindingFlags.Instance | BindingFlags.Public);
        bool hasOneMatch = false;

        foreach (var ci in cultures)
        {
            bool match = true;
            // Get the DateTimeFormatInfo for a specific culture.
            DateTimeFormatInfo specificDtfi = ci.DateTimeFormat;
            // Compare the property values of the two.
            foreach (var prop in properties)
            {
                // We're not interested in the value of IsReadOnly.
                if (prop.Name == "IsReadOnly") continue;

                // For arrays, iterate the individual elements to see if they are the same.
                if (prop.PropertyType.IsArray)
                {
                    IList nList = (IList)prop.GetValue(dtfi, null);
                    IList sList = (IList)prop.GetValue(specificDtfi, null);
                    if (nList.Count != sList.Count)
                    {
                        match = false;
                        Console.WriteLine("   Different n in {2} array for {0} and {1}", name, ci.Name, prop.Name);
                        break;
                    }

                    for (int ctr = 0; ctr < nList.Count; ctr++)
                    {
                        if (!nList[ctr].Equals(sList[ctr]))
                        {
                            match = false;
                            Console.WriteLine("   {0} value different for {1} and {2}", prop.Name, name, ci.Name);
                            break;
                        }
                    }

                    if (!match) break;
                }
                // Get non-array values.
                else
                {
                    Object specificValue = prop.GetValue(specificDtfi);
                    Object neutralValue = prop.GetValue(dtfi);

                    // Handle comparison of Calendar objects.
                    if (prop.Name == "Calendar")
                    {
                        // The cultures have a different calendar type.
                        if (specificValue.ToString() != neutralValue.ToString())
                        {
                            Console.WriteLine("   Different calendar types for {0} and {1}", name, ci.Name);
                            match = false;
                            break;
                        }

                        if (specificValue is GregorianCalendar)
                        {
                            if (((GregorianCalendar)specificValue).CalendarType != ((GregorianCalendar)neutralValue).CalendarType)
                            {
                                Console.WriteLine("   Different Gregorian calendar types for {0} and {1}", name, ci.Name);
                                match = false;
                                break;
                            }
                        }
                    }
                    else if (!specificValue.Equals(neutralValue))
                    {
                        match = false;
                        Console.WriteLine("   Different {0} values for {1} and {2}", prop.Name, name, ci.Name);
                        break;
                    }
                }
            }
            if (match)
            {
                Console.WriteLine("DateTimeFormatInfo object for '{0}' matches '{1}'",
                                  name, ci.Name);
                hasOneMatch = true;
            }
        }
        if (!hasOneMatch)
            Console.WriteLine("DateTimeFormatInfo object for '{0}' --> No Match", name);

        Console.WriteLine();
    }
}

具現化目前文化特性的 DateTimeFormatInfo 物件

您可以具現化 DateTimeFormatInfo 物件,以下列方式呈現目前文化特性的格式設定慣例:

下列範例會使用上述每一 DateTimeFormatInfo 種方法來具現化物件,此物件代表目前文化特性的格式設定慣例。 然後,它會指出物件是否為唯讀。

DateTimeFormatInfo dtfi;

dtfi = DateTimeFormatInfo.CurrentInfo;
Console.WriteLine(dtfi.IsReadOnly);

dtfi = CultureInfo.CurrentCulture.DateTimeFormat;
Console.WriteLine(dtfi.IsReadOnly);

dtfi = DateTimeFormatInfo.GetInstance(CultureInfo.CurrentCulture);
Console.WriteLine(dtfi.IsReadOnly);
// The example displays the following output:
//     True
//     True
//     True

您可以建立可 DateTimeFormatInfo 寫入的物件,以下列其中一種方式來代表目前文化特性的慣例:

下列範例說明如何具現化讀取/寫入 DateTimeFormatInfo 物件,並顯示其 IsReadOnly 屬性的值。

using System;
using System.Globalization;

public class InstantiateEx1
{
    public static void Main()
    {
        DateTimeFormatInfo current1 = DateTimeFormatInfo.CurrentInfo;
        current1 = (DateTimeFormatInfo)current1.Clone();
        Console.WriteLine(current1.IsReadOnly);

        CultureInfo culture2 = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
        DateTimeFormatInfo current2 = culture2.DateTimeFormat;
        Console.WriteLine(current2.IsReadOnly);
    }
}
// The example displays the following output:
//       False
//       False

在 Windows 中,使用者可以透過 控制台 中的 Region 和 Language 應用程式覆寫用於格式化和剖析作業的一些DateTimeFormatInfo屬性值。 例如,文化特性為英文(美國)的使用者可能會選擇使用 24 小時制來顯示長時間值(格式為 HH:mm:ss),而不是預設的 12 小時制(格式為 h:mm:ss tt)。 以 DateTimeFormatInfo 先前所討論方式擷取的物件都會反映這些使用者覆寫。 如果這是不想要的,您可以藉由呼叫 CultureInfo.CultureInfo(String, Boolean) 建構函式並提供 自變數的值false,建立NumberFormatInfo不會反映使用者覆寫的物件(而且也是可擦寫的,而不是只讀的useUserOverride)。 下列範例說明目前文化特性為英文(美國)且長時間模式已從 h:mm:ss tt 的預設值變更為 HH:mm:ss 的系統。

using System;
using System.Globalization;

public class InstantiateEx3
{
    public static void Main()
    {
        CultureInfo culture;
        DateTimeFormatInfo dtfi;

        culture = CultureInfo.CurrentCulture;
        dtfi = culture.DateTimeFormat;
        Console.WriteLine("Culture Name:      {0}", culture.Name);
        Console.WriteLine("User Overrides:    {0}", culture.UseUserOverride);
        Console.WriteLine("Long Time Pattern: {0}\n", culture.DateTimeFormat.LongTimePattern);

        culture = new CultureInfo(CultureInfo.CurrentCulture.Name, false);
        Console.WriteLine("Culture Name:      {0}", culture.Name);
        Console.WriteLine("User Overrides:    {0}", culture.UseUserOverride);
        Console.WriteLine("Long Time Pattern: {0}\n", culture.DateTimeFormat.LongTimePattern);
    }
}
// The example displays the following output:
//       Culture Name:      en-US
//       User Overrides:    True
//       Long Time Pattern: HH:mm:ss
//
//       Culture Name:      en-US
//       User Overrides:    False
//       Long Time Pattern: h:mm:ss tt

DateTimeFormatInfo 和動態數據

格式化 類別所 DateTimeFormatInfo 提供日期和時間值的文化特性特定數據是動態的,就像 類別所提供的 CultureInfo 文化特性數據一樣。 您不應該對與特定CultureInfo對象相關聯之物件的值DateTimeFormatInfo穩定性進行任何假設。 只有不因文化特性及其相關聯 DateTimeFormatInfo 物件所提供的數據是穩定的。 其他數據可以在應用程式會話之間變更,甚至是在應用程式執行時變更。 有四個主要變更來源:

  • 系統更新。 文化特性喜好設定,例如慣用的行事曆或慣用日期和時間格式會隨著時間而變更。 發生這種情況時,Windows Update 會包含特定文化特性屬性值的變更 DateTimeFormatInfo

  • 取代文化特性。 類別 CultureAndRegionInfoBuilder 可用來取代現有文化特性的數據。

  • 屬性值的串聯變更。 一些文化特性相關的屬性可以在運行時間變更,這反過來又會導致 DateTimeFormatInfo 數據變更。 例如,您可以透過程序設計方式或透過使用者動作來變更目前的文化特性。 發生這種情況時, DateTimeFormatInfo 屬性所傳 CurrentInfo 回的物件會變更為與目前文化特性相關聯的物件。 同樣地,文化特性的行事歷可能會變更,這可能會導致許多 DateTimeFormatInfo 屬性值的變更。

  • 用戶喜好設定。 您的應用程式使用者可能會選擇透過 控制台 中的地區和語言選項,覆寫與目前系統文化特性相關聯的部分值。 例如,使用者可能會選擇以不同的格式顯示日期。 CultureInfo.UseUserOverride如果 屬性設定為 true,對象的屬性DateTimeFormatInfo也會從用戶設定中擷取。 如果使用者設定與對象相關聯的 CultureInfo 文化特性不相容(例如,如果選取的行事曆不是 屬性所指出 OptionalCalendars 的其中一個行事歷),則方法和屬性值未定義。

若要將不一致數據的可能性降到最低,建立物件時,就會初始化物件的所有使用者可覆寫屬性 DateTimeFormatInfo 。 仍有不一致的可能性,因為物件建立和使用者覆寫程式都不可部分完成,相關值在物件建立期間可能會變更。 不過,這種情況應該非常罕見。

您可以控制使用者覆寫是否反映在 DateTimeFormatInfo 表示與系統文化特性相同文化特性的物件中。 下表列出可以擷取物件的方式 DateTimeFormatInfo ,並指出產生的物件是否反映使用者覆寫。

CultureInfo 和 DateTimeFormatInfo 物件的來源 反思 用戶覆寫
CultureInfo.CurrentCulture.DateTimeFormat 屬性 Yes
DateTimeFormatInfo.CurrentInfo 屬性 Yes
CultureInfo.CreateSpecificCulture 方法 Yes
CultureInfo.GetCultureInfo 方法 No
CultureInfo.CultureInfo(String) 建構函式 Yes
CultureInfo.CultureInfo(String, Boolean) 建構函式 取決於參數的值useUserOverride

除非有令人信服的理由這樣做,否則當您在 DateTimeFormatInfo 用戶端應用程式中使用 物件來格式化和剖析使用者輸入或顯示數據時,您應該遵守使用者覆寫。 對於伺服器應用程式或自動應用程式,您不應該。 不過,如果您明確或隱含地使用 DateTimeFormatInfo 物件來保存字串格式的日期和時間數據,則應該使用 DateTimeFormatInfo 反映不因文化特性之格式設定慣例的物件,或者您應該指定不論文化特性為何使用的自定義日期和時間格式字元串。

格式化日期和時間

DateTimeFormatInfo物件會在所有日期和時間格式設定作業中隱含或明確使用。 這些包括呼叫下列方法:

所有日期和時間格式設定作業都會使用 實 IFormatProvider 作。 介面 IFormatProvider 包含單一方法 IFormatProvider.GetFormat(Type)。 這個回呼方法會傳遞 Type 物件,此物件表示提供格式資訊所需的類型。 方法會傳回該型別的實例,如果 null 無法提供型別的實例,則傳回 。 .NET 包含兩 IFormatProvider 個格式化日期和時間的實作:

IFormatProvider如果未明確將實作提供給格式化方法,CultureInfo則會使用表示目前文化特性的屬性所CultureInfo.CurrentCulture傳回的物件。

下列範例說明在格式化作業中 IFormatProvider 介面與 DateTimeFormatInfo 類別之間的關聯性。 它會定義自定義 IFormatProvider 實作,其 GetFormat 方法會顯示格式化作業所要求的物件類型。 如果它要求 DateTimeFormatInfo 物件,方法會提供 DateTimeFormatInfo 目前文化特性的物件。 如範例的輸出所示, Decimal.ToString(IFormatProvider) 方法會要求 DateTimeFormatInfo 物件提供格式化資訊,而 String.Format(IFormatProvider, String, Object[]) 方法會要求 NumberFormatInfoDateTimeFormatInfo 物件以及 ICustomFormatter 實作。

using System;
using System.Globalization;

public class CurrentCultureFormatProvider : IFormatProvider
{
    public Object GetFormat(Type formatType)
    {
        Console.WriteLine("Requesting an object of type {0}",
                          formatType.Name);
        if (formatType == typeof(NumberFormatInfo))
            return NumberFormatInfo.CurrentInfo;
        else if (formatType == typeof(DateTimeFormatInfo))
            return DateTimeFormatInfo.CurrentInfo;
        else
            return null;
    }
}

public class FormatProviderEx1
{
    public static void Main()
    {
        DateTime dateValue = new DateTime(2013, 5, 28, 13, 30, 0);
        string value = dateValue.ToString("F", new CurrentCultureFormatProvider());
        Console.WriteLine(value);
        Console.WriteLine();
        string composite = String.Format(new CurrentCultureFormatProvider(),
                                         "Date: {0:d}   Amount: {1:C}   Description: {2}",
                                         dateValue, 1264.03m, "Service Charge");
        Console.WriteLine(composite);
        Console.WriteLine();
    }
}
// The example displays output like the following:
//       Requesting an object of type DateTimeFormatInfo
//       Tuesday, May 28, 2013 1:30:00 PM
//
//       Requesting an object of type ICustomFormatter
//       Requesting an object of type DateTimeFormatInfo
//       Requesting an object of type NumberFormatInfo
//       Date: 5/28/2013   Amount: $1,264.03   Description: Service Charge

格式化字串和 DateTimeFormatInfo 屬性

物件 DateTimeFormatInfo 包含三種屬性,這些屬性用於格式化具有日期和時間值的作業:

標準日期和時間格式字串,例如 「d」、“D”、“f” 和 “F”,都是對應至特定DateTimeFormatInfo格式模式屬性的別名。 大部分的自定義日期和時間格式字串都與格式化作業插入結果數據流的字串或子字串串相關。 下表列出標準和自定義日期和時間格式規範及其相關聯的 DateTimeFormatInfo 屬性。 如需如何使用這些格式規範的詳細資訊,請參閱 標準日期和時間格式字串自定義日期和時間格式字串。 請注意,每個標準格式字串都會對應至 DateTimeFormatInfo 值為自定義日期和時間格式字串的屬性。 這個自定義格式字串中的個別規範會接著對應至其他 DateTimeFormatInfo 屬性。 數據表只會 DateTimeFormatInfo 列出標準格式字串為別名的屬性,而且不會列出可由指派給這些別名屬性的自定義格式字串存取的屬性。 此外,數據表只會列出對應至 DateTimeFormatInfo 屬性的自定義格式規範。

格式規範 相關聯的屬性
“d” (簡短日期;標準格式字串) ShortDatePattern,以定義結果字串的整體格式。
“D” (長日期;標準格式字串) LongDatePattern,以定義結果字串的整體格式。
“f” (完整日期/短時間;標準格式字串) LongDatePattern,以定義結果字串的日期元件格式。

ShortTimePattern,以定義結果字串的時間元件格式。
“F” (完整日期/長時間;標準格式字串) LongDatePattern,以定義結果字串的日期元件格式。

LongTimePattern,以定義結果字串的時間元件格式。
“g” (一般日期/短時間;標準格式字串) ShortDatePattern,以定義結果字串的日期元件格式。

ShortTimePattern,以定義結果字串的時間元件格式。
“G” (一般日期/長時間;標準格式字串) ShortDatePattern,以定義結果字串的日期元件格式。

LongTimePattern,以定義結果字串的時間元件格式。
“M”、“m” (月/日;標準格式字串) MonthDayPattern,以定義結果字串的整體格式。
“O”, “o” (來回日期/時間;標準格式字串) 無。
“R”、“r” (RFC1123;標準格式字串) RFC1123Pattern,以定義符合 RFC 1123 標準的結果字串。 此為唯讀屬性。
“s” (可排序的日期/時間;標準格式字串) SortableDateTimePattern,以定義符合 ISO 8601 標準的結果字串。 此為唯讀屬性。
“t” (短時間;標準格式字串) ShortTimePattern,以定義結果字串的整體格式。
“T” (長時間;標準格式字串) LongTimePattern,以定義結果字串的整體格式。
“u” (通用可排序日期/時間;標準格式字串) UniversalSortableDateTimePattern,以定義符合 ISO 8601 標準以協調通用時間的結果字串。 此為唯讀屬性。
“U” (通用完整日期/時間;標準格式字串) FullDateTimePattern,以定義結果字串的整體格式。
“Y”, “y” (年月; 標準格式字串) YearMonthPattern,以定義結果字串的整體格式。
“ddd” (自定義格式規範) AbbreviatedDayNames,表示在結果字串中包含星期天的縮寫名稱。
“g”、“gg” (自定義格式規範) GetEraName呼叫 方法,以在結果字串中插入紀元名稱。
“MMM” (自定義格式規範) AbbreviatedMonthNames,表示在結果字串中包含縮寫月份名稱。
“MMMM” (自定義格式規範) MonthNamesMonthGenitiveNames,表示在結果字串中包含完整月份名稱。
“t” (自定義格式規範) AMDesignatorPMDesignator,表示在結果字串中包含AM/PM指示項的第一個字元。
“tt” (自定義格式規範) AMDesignatorPMDesignator,表示在結果字串中包含完整的AM/PM指示項。
“:” (自定義格式規範) TimeSeparator,表示在結果字串中包含時間分隔符。
“/” (自定義格式規範) DateSeparator,表示在結果字串中包含日期分隔符。

修改 DateTimeFormatInfo 屬性

您可以修改可 DateTimeFormatInfo 寫入物件的相關聯屬性,以變更日期和時間格式字串所產生的結果字串。 若要判斷物件是否 DateTimeFormatInfo 可寫入,請使用 IsReadOnly 屬性。 若要以這種方式自訂 DateTimeFormatInfo 物件:

  1. 建立您想要修改其格式慣例的物件 DateTimeFormatInfo 讀取/寫入複本。

  2. 修改用來產生所需結果字串的屬性或屬性。 (如需格式化方法 DateTimeFormatInfo 如何使用屬性來定義結果字串的資訊,請參閱上一節: 格式化字串和 DateTimeFormatInfo 屬性。)

  3. 使用您在呼叫格式化方法時建立為自變數的IFormatProvider自定義DateTimeFormatInfo物件。

還有兩種方式可以變更結果字串的格式:

  • 您可以使用 類別 CultureAndRegionInfoBuilder 來定義自定義文化特性(具有唯一名稱和補充現有文化特性的文化特性)或取代文化特性(使用而非特定文化特性的文化特性)。 您可以透過程式設計方式儲存並存取此文化特性,就像 .NET 所支援的任何 CultureInfo 對象一樣。

  • 如果結果字串不區分文化特性,且未遵循預先定義的格式,您可以使用自定義日期和時間格式字串。 例如,如果您要以YYYYMMDDDHMMss格式串行化日期和時間數據,您可以將自定義格式字串傳遞至 DateTime.ToString(String) 方法來產生結果字串,而您可以呼叫 DateTime.ParseExact 方法,將結果字串DateTime轉換成值。

變更簡短日期模式

下列範例會變更 「d」 (short date) 標準格式字串所產生的結果字串格式。 它會將 en-US 或 English (美國) 文化特性的關聯ShortDatePattern屬性從預設值 “M/d/y” 變更為 “yyyy'-”MM“-”dd“,並使用 ”d“ 標準格式字符串來顯示屬性變更前後的ShortDatePattern日期。

using System;
using System.Globalization;

public class Example1
{
    public static void Main()
    {
        DateTime dateValue = new DateTime(2013, 8, 18);
        CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
        DateTimeFormatInfo dtfi = enUS.DateTimeFormat;

        Console.WriteLine("Before modifying DateTimeFormatInfo object: ");
        Console.WriteLine("{0}: {1}\n", dtfi.ShortDatePattern,
                                      dateValue.ToString("d", enUS));

        // Modify the short date pattern.
        dtfi.ShortDatePattern = "yyyy-MM-dd";
        Console.WriteLine("After modifying DateTimeFormatInfo object: ");
        Console.WriteLine("{0}: {1}", dtfi.ShortDatePattern,
                                      dateValue.ToString("d", enUS));
    }
}
// The example displays the following output:
//       Before modifying DateTimeFormatInfo object:
//       M/d/yyyy: 8/18/2013
//
//       After modifying DateTimeFormatInfo object:
//       yyyy-MM-dd: 2013-08-18

變更日期分隔符

下列範例會變更 物件中的 DateTimeFormatInfo 日期分隔符,代表fr-FR文化特性的格式設定慣例。 此範例會使用 「g」 標準格式字串來顯示屬性變更前後的 DateSeparator 日期。

using System;
using System.Globalization;

public class Example3
{
    public static void Main()
    {
        DateTime dateValue = new DateTime(2013, 08, 28);
        CultureInfo frFR = CultureInfo.CreateSpecificCulture("fr-FR");
        DateTimeFormatInfo dtfi = frFR.DateTimeFormat;

        Console.WriteLine("Before modifying DateSeparator property: {0}",
                          dateValue.ToString("g", frFR));

        // Modify the date separator.
        dtfi.DateSeparator = "-";
        Console.WriteLine("After modifying the DateSeparator property: {0}",
                          dateValue.ToString("g", frFR));
    }
}
// The example displays the following output:
//       Before modifying DateSeparator property: 28/08/2013 00:00
//       After modifying the DateSeparator property: 28-08-2013 00:00

變更日期名稱縮寫和長日期模式

在某些情況下,完整日期模式通常會顯示完整日和月份名稱,以及月份和年份的日數,可能太長。 下列範例會縮短 en-US 文化特性的長日期模式,以傳回一個字元或兩個字元的日期名稱縮寫,後面接著日號、月份名稱縮寫和年份。 它會藉由將較短的日名稱縮寫指派給 AbbreviatedDayNames 陣列,以及修改指派給屬性的 LongDatePattern 自定義格式字串,來執行這項作業。 這會影響 「D」 和 「f」 標準格式字串所傳回的結果字串。

using System;
using System.Globalization;

public class Example2
{
    public static void Main()
    {
        DateTime value = new DateTime(2013, 7, 9);
        CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
        DateTimeFormatInfo dtfi = enUS.DateTimeFormat;
        String[] formats = { "D", "F", "f" };

        // Display date before modifying properties.
        foreach (var fmt in formats)
            Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi));

        Console.WriteLine();

        // We don't want to change the FullDateTimePattern, so we need to save it.
        String originalFullDateTimePattern = dtfi.FullDateTimePattern;

        // Modify day name abbreviations and long date pattern.
        dtfi.AbbreviatedDayNames = new String[] { "Su", "M", "Tu", "W", "Th", "F", "Sa" };
        dtfi.LongDatePattern = "ddd dd-MMM-yyyy";
        dtfi.FullDateTimePattern = originalFullDateTimePattern;
        foreach (var fmt in formats)
            Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi));
    }
}
// The example displays the following output:
//       D: Tuesday, July 9, 2013
//       F: Tuesday, July 9, 2013 12:00:00 AM
//       f: Tuesday, July 9, 2013 12:00 AM
//
//       D: Tu 09-Jul-2013
//       F: Tuesday, July 9, 2013 12:00:00 AM
//       f: Tu 09-Jul-2013 12:00 AM

一般而言,對屬性的 LongDatePattern 變更也會影響 FullDateTimePattern 屬性,進而定義 「F」 標準格式字串所傳回的結果字串。 為了保留原始的完整日期和時間模式,此範例會在修改 屬性之後LongDatePattern,重新指派指派給 FullDateTimePattern 屬性的原始自定義格式字串。

從 12 小時制變更為 24 小時制

對於 .NET 中的許多文化特性,時間是使用 12 小時制和 AM/PM 指示項來表示。 下列範例會 ReplaceWith24HourClock 定義方法,這個方法會取代使用12小時制的任何時間格式,以及使用24小時制的格式。

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example5
{
    public static void Main()
    {
        CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
        DateTimeFormatInfo dtfi = enUS.DateTimeFormat;

        Console.WriteLine("Original Property Values:");
        Console.WriteLine("ShortTimePattern: " + dtfi.ShortTimePattern);
        Console.WriteLine("LongTimePattern: " + dtfi.LongTimePattern);
        Console.WriteLine("FullDateTimePattern: " + dtfi.FullDateTimePattern);
        Console.WriteLine();

        dtfi.LongTimePattern = ReplaceWith24HourClock(dtfi.LongTimePattern);
        dtfi.ShortTimePattern = ReplaceWith24HourClock(dtfi.ShortTimePattern);

        Console.WriteLine("Modififed Property Values:");
        Console.WriteLine("ShortTimePattern: " + dtfi.ShortTimePattern);
        Console.WriteLine("LongTimePattern: " + dtfi.LongTimePattern);
        Console.WriteLine("FullDateTimePattern: " + dtfi.FullDateTimePattern);
    }

    private static string ReplaceWith24HourClock(string fmt)
    {
        string pattern = @"^(?<openAMPM>\s*t+\s*)? " +
                         @"(?(openAMPM) h+(?<nonHours>[^ht]+)$ " +
                         @"| \s*h+(?<nonHours>[^ht]+)\s*t+)";
        return Regex.Replace(fmt, pattern, "HH${nonHours}",
                             RegexOptions.IgnorePatternWhitespace);
    }
}
// The example displays the following output:
//       Original Property Values:
//       ShortTimePattern: h:mm tt
//       LongTimePattern: h:mm:ss tt
//       FullDateTimePattern: dddd, MMMM dd, yyyy h:mm:ss tt
//
//       Modififed Property Values:
//       ShortTimePattern: HH:mm
//       LongTimePattern: HH:mm:ss
//       FullDateTimePattern: dddd, MMMM dd, yyyy HH:mm:ss

此範例會使用正則表示式來修改格式字串。 正規表示式模式 @"^(?<openAMPM>\s*t+\s*)? (?(openAMPM) h+(?<nonHours>[^ht]+)$ | \s*h+(?<nonHours>[^ht]+)\s*t+) 的定義如下:

模式 描述
^ 從字串的開頭開始比對。
(?<openAMPM>\s*t+\s*)? 比對零或一次出現零或多個空格符,後面接著字母 「t」 一或多次,後面接著零個或多個空格符。 這個擷取群組名為 openAMPM
(?(openAMPM) h+(?<nonHours>[^ht]+)$ openAMPM如果群組有相符專案,請比對字母 「h」 一或多次,後面接著一或多個不是 「h」 或 「t」 的字元。 比對會在字串結尾結束。 在 「h」 之後擷取的所有字元都會包含在名為 nonHours的擷取群組中。
&#124; \s*h+(?<nonHours>[^ht]+)\s*t+) openAMPM如果群組沒有相符專案,請比對字母 「h」 一或多次,後面接著一或多個不是 「h」 或 「t」 的字元,後面接著零個或多個空格符。 最後,比對字母 「t」 的一或多個專案。 在 「h」 之後和空格符和 「t」 之前擷取的所有字元都會包含在名為 nonHours的擷取群組中。

nonHours擷取群組包含自訂日期和時間格式字串的分鐘和第二個元件,以及任何時間分隔符符號。 取代模式 HH${nonHours} 會將子字串 「HH」 前面加上這些元素。

顯示及變更日期中的紀元

下列範例會將 「g」 自訂格式規範新增至 LongDatePattern 對象的 屬性,此物件代表 en-US 文化特性的格式設定慣例。 此新增會影響下列三個標準格式字串:

  • “D” (long date) 標準格式字串,其會直接對應至 LongDatePattern 屬性。

  • “f” (完整日期/短時間)標準格式字串,其會產生結果字串,以串連 和 ShortTimePattern 屬性所產生的LongDatePattern子字串。

  • 直接對應至 FullDateTimePattern 屬性的 「F」 (完整日期/長時間)標準格式字串。 因為我們尚未明確設定這個屬性值,所以會藉由串連 LongDatePatternLongTimePattern 屬性,以動態方式產生此值。

此範例也會示範如何變更行事曆具有單一紀元的文化特性紀元名稱。 在此情況下,en-US 文化特性會使用以 GregorianCalendar 物件表示的公曆。 類別 GregorianCalendar 支援單一紀元,其命名為 A.D. (Anno Domini)。 此範例會以常值字串取代指派給 FullDateTimePattern 屬性的格式字串中的 “g” 自定義格式規範,將紀元名稱變更為 C.E. (Common Era)。 使用常值字串是必要的,因為紀元名稱通常是由 GetEraName 方法從 .NET 或操作系統所提供的文化特性數據表中的私用數據傳回。

using System;
using System.Globalization;

public class Example4
{
    public static void Main()
    {
        DateTime dateValue = new DateTime(2013, 5, 18, 13, 30, 0);
        String[] formats = { "D", "f", "F" };

        CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
        DateTimeFormatInfo dtfi = enUS.DateTimeFormat;
        String originalLongDatePattern = dtfi.LongDatePattern;

        // Display the default form of three long date formats.
        foreach (var fmt in formats)
            Console.WriteLine(dateValue.ToString(fmt, dtfi));

        Console.WriteLine();

        // Modify the long date pattern.
        dtfi.LongDatePattern = originalLongDatePattern + " g";
        foreach (var fmt in formats)
            Console.WriteLine(dateValue.ToString(fmt, dtfi));

        Console.WriteLine();

        // Change A.D. to C.E. (for Common Era)
        dtfi.LongDatePattern = originalLongDatePattern + @" 'C.E.'";
        foreach (var fmt in formats)
            Console.WriteLine(dateValue.ToString(fmt, dtfi));
    }
}
// The example displays the following output:
//       Saturday, May 18, 2013
//       Saturday, May 18, 2013 1:30 PM
//       Saturday, May 18, 2013 1:30:00 PM
//
//       Saturday, May 18, 2013 A.D.
//       Saturday, May 18, 2013 A.D. 1:30 PM
//       Saturday, May 18, 2013 A.D. 1:30:00 PM
//
//       Saturday, May 18, 2013 C.E.
//       Saturday, May 18, 2013 C.E. 1:30 PM
//       Saturday, May 18, 2013 C.E. 1:30:00 PM

剖析日期和時間字串

剖析牽涉到將日期和時間的字串表示轉換為 DateTimeDateTimeOffset 值。 這兩種類型都包含 ParseTryParseParseExactTryParseExact 方法來支援剖析作業。 ParseTryParse 方法會轉換可具有各種格式的字串,而 ParseExact 而且TryParseExact需要字串具有定義的格式或格式。 如果剖析作業失敗, ParseParseExact 擲回例外狀況,而 TryParseTryParseExact 傳回 false

剖析方法隱含或明確使用 DateTimeStyles 列舉值來判斷要剖析的字串中可以存在哪些樣式專案(例如前置、尾端或內部空格符),以及如何解譯剖析的字串或任何遺漏的專案。 如果您在呼叫 Parse 或 方法時未提供DateTimeStyles值,則預設值為 DateTimeStyles.AllowWhiteSpaces,這是包含 DateTimeStyles.AllowLeadingWhiteDateTimeStyles.AllowTrailingWhiteDateTimeStyles.AllowInnerWhite 旗標的複合TryParse樣式。 ParseExact針對 和 TryParseExact 方法,預設值為 DateTimeStyles.None;輸入字串必須精確對應至特定的自定義日期和時間格式字串。

剖析方法也會隱含或明確使用 DateTimeFormatInfo 物件,定義可剖析之字串中可能發生的特定符號和模式。 如果您未提供 DateTimeFormatInfo 對象, DateTimeFormatInfo 預設會使用目前文化特性的物件。 如需剖析日期和時間字串的詳細資訊,請參閱個別剖析方法,例如 DateTime.ParseDateTime.TryParseDateTimeOffset.ParseExactDateTimeOffset.TryParseExact

下列範例說明剖析日期和時間字串的文化特性。 它會使用 en-US、en-GB、fr-FR 和 fi-FI 文化特性的慣例,嘗試剖析兩個日期字串。 在 en-US 文化特性中解譯為 8/18/2014 的日期會擲回其他三個 FormatException 文化特性中的例外狀況,因為 18 會解譯為月份號碼。 2015年1月2日會剖析為美國文化特性中第一個月的第二天,但作為剩餘文化特性中第二個月的第一天。

using System;
using System.Globalization;

public class ParseEx1
{
    public static void Main()
    {
        string[] dateStrings = { "08/18/2014", "01/02/2015" };
        string[] cultureNames = { "en-US", "en-GB", "fr-FR", "fi-FI" };

        foreach (var cultureName in cultureNames)
        {
            CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
            Console.WriteLine("Parsing strings using the {0} culture.",
                              culture.Name);
            foreach (var dateStr in dateStrings)
            {
                try
                {
                    Console.WriteLine(String.Format(culture,
                                      "   '{0}' --> {1:D}", dateStr,
                                      DateTime.Parse(dateStr, culture)));
                }
                catch (FormatException)
                {
                    Console.WriteLine("   Unable to parse '{0}'", dateStr);
                }
            }
        }
    }
}
// The example displays the following output:
//       Parsing strings using the en-US culture.
//          '08/18/2014' --> Monday, August 18, 2014
//          '01/02/2015' --> Friday, January 02, 2015
//       Parsing strings using the en-GB culture.
//          Unable to parse '08/18/2014'
//          '01/02/2015' --> 01 February 2015
//       Parsing strings using the fr-FR culture.
//          Unable to parse '08/18/2014'
//          '01/02/2015' --> dimanche 1 février 2015
//       Parsing strings using the fi-FI culture.
//          Unable to parse '08/18/2014'
//          '01/02/2015' --> 1. helmikuuta 2015

日期和時間字串通常會剖析兩個原因:

  • 若要將使用者輸入轉換成日期和時間值。
  • 若要往返日期和時間值;也就是說,還原串行化先前串行化為字串的日期和時間值。

下列各節會更詳細地討論這兩項作業。

剖析使用者字串

當您剖析使用者的日期和時間字串輸入時,應該一律具現化 DateTimeFormatInfo 反映使用者文化設定的物件,包括使用者可能所做的任何自定義。 否則,日期和時間物件可能會有不正確的值。 如需如何具現化 DateTimeFormatInfo 反映使用者文化自定義之對象的資訊,請參閱 DateTimeFormatInfo 和動態數據 一節。

下列範例說明剖析作業之間的差異,該作業反映使用者文化特性設定,以及未反映的作業。 在此情況下,默認系統文化特性為 en-US,但使用者已使用 控制台、Region 和 Language 將簡短日期模式從預設值 “M/d/yyyy” 變更為 “yy/MM/dd”。 當使用者輸入反映使用者設定的字串,而且字串是由 DateTimeFormatInfo 也會反映使用者設定(覆寫)的物件剖析時,剖析作業會傳回正確的結果。 不過,當字串由 DateTimeFormatInfo 反映標準 en-US 文化設定的物件剖析時,剖析方法會擲回 FormatException 例外狀況,因為它會將 14 解譯為月份的數目,而不是年份的最後兩位數。

using System;
using System.Globalization;

public class ParseEx2
{
    public static void Main()
    {
        string inputDate = "14/05/10";

        CultureInfo[] cultures = { CultureInfo.GetCultureInfo("en-US"),
                                 CultureInfo.CreateSpecificCulture("en-US") };

        foreach (var culture in cultures)
        {
            try
            {
                Console.WriteLine("{0} culture reflects user overrides: {1}",
                                  culture.Name, culture.UseUserOverride);
                DateTime occasion = DateTime.Parse(inputDate, culture);
                Console.WriteLine("'{0}' --> {1}", inputDate,
                                  occasion.ToString("D", CultureInfo.InvariantCulture));
            }
            catch (FormatException)
            {
                Console.WriteLine("Unable to parse '{0}'", inputDate);
            }
            Console.WriteLine();
        }
    }
}
// The example displays the following output:
//       en-US culture reflects user overrides: False
//       Unable to parse '14/05/10'
//
//       en-US culture reflects user overrides: True
//       '14/05/10' --> Saturday, 10 May 2014

串行化和還原串行化日期和時間數據

串行化日期和時間數據預期會往返;也就是說,所有串行化和還原串行化值都應該相同。 如果日期和時間值代表單一時間刻,則還原串行化值應該代表時間中的相同時刻,不論其還原所在的系統文化特性或時區為何。 若要成功往返日期和時間數據,您必須使用 屬性傳 InvariantInfo 回的不因文化特性慣例來產生和剖析數據。 格式設定和剖析作業絕不應反映預設文化特性的慣例。 如果您使用預設的文化設定,數據可移植性會受到嚴格限制;它只能在文化特性特定設定與串行化線程相同的線程上成功還原串行化。 在某些情況下,這表示數據甚至無法在相同系統上成功串行化和還原串行化。

如果日期和時間值的時間元件很重要,它也應該轉換成UTC,並使用 「o」 或「r」 標準格式字串進行串行化。 然後可以藉由呼叫剖析方法來還原時間數據,並將適當的格式字串連同不變異文化特性傳遞為 provider 自變數。

下列範例說明往返日期和時間值的程式。 它會在觀察美國太平洋時間且目前文化特性為 en-US 的系統上串行化日期和時間。

using System;
using System.Globalization;
using System.IO;

public class SerializeEx1
{
    public static void Main()
    {
        StreamWriter sw = new StreamWriter(@".\DateData.dat");
        // Define a date and time to serialize.
        DateTime originalDate = new DateTime(2014, 08, 18, 08, 16, 35);
        // Display information on the date and time.
        Console.WriteLine("Date to serialize: {0:F}", originalDate);
        Console.WriteLine("Current Culture:   {0}",
                          CultureInfo.CurrentCulture.Name);
        Console.WriteLine("Time Zone:         {0}",
                          TimeZoneInfo.Local.DisplayName);
        // Convert the date value to UTC.
        DateTime utcDate = originalDate.ToUniversalTime();
        // Serialize the UTC value.
        sw.Write(utcDate.ToString("o", DateTimeFormatInfo.InvariantInfo));
        sw.Close();
    }
}
// The example displays the following output:
//       Date to serialize: Monday, August 18, 2014 8:16:35 AM
//       Current Culture:   en-US
//       Time Zone:         (UTC-08:00) Pacific Time (US & Canada)

它會還原串行化布魯塞爾、哥本哈根、馬德里和巴黎時區系統上的數據,其目前的文化特性為fr-FR。 還原的日期比原始日期晚九小時,反映時區調整從UTC落後8小時到UTC之前的一小時。 原始日期與還原的日期都代表相同的時間刻度。

using System;
using System.Globalization;
using System.IO;

public class SerializeEx2
{
    public static void Main()
    {
        // Open the file and retrieve the date string.
        StreamReader sr = new StreamReader(@".\DateData.dat");
        String dateValue = sr.ReadToEnd();

        // Parse the date.
        DateTime parsedDate = DateTime.ParseExact(dateValue, "o",
                              DateTimeFormatInfo.InvariantInfo);
        // Convert it to local time.
        DateTime restoredDate = parsedDate.ToLocalTime();
        // Display information on the date and time.
        Console.WriteLine("Deserialized date: {0:F}", restoredDate);
        Console.WriteLine("Current Culture:   {0}",
                          CultureInfo.CurrentCulture.Name);
        Console.WriteLine("Time Zone:         {0}",
                          TimeZoneInfo.Local.DisplayName);
    }
}
// The example displays the following output:
//    Deserialized date: lundi 18 août 2014 17:16:35
//    Current Culture:   fr-FR
//    Time Zone:         (UTC+01:00) Brussels, Copenhagen, Madrid, Paris