DateTime.ParseExact 方法

定義

將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須完全符合指定的格式,否則會擲回例外狀況。

多載

ParseExact(String, String, IFormatProvider)

使用指定的格式以及特定文化特性的格式資訊,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須完全符合指定的格式。

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

使用指定的格式、特定文化特性格式資訊以及樣式,以將日期和時間的指定範圍表示轉換為與其相等的 DateTime。 字串表示的格式必須完全符合指定的格式,否則會擲回例外狀況。

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)

使用指定格式、特定文化特性格式資訊以及樣式的陣列,以將日期和時間的指定範圍表示轉換為與其相等的 DateTime。 字串表示的格式必須至少完全符合其中一個指定的格式,否則會擲回例外狀況。

ParseExact(String, String, IFormatProvider, DateTimeStyles)

使用指定的格式、特定文化特性格式資訊以及樣式,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須完全符合指定的格式,否則會擲回例外狀況。

ParseExact(String, String[], IFormatProvider, DateTimeStyles)

使用指定的格式陣列、特定文化特性格式資訊以及樣式,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須至少完全符合其中一個指定的格式,否則會擲回例外狀況。

備註

重要

日本曆法的紀元是以天皇的統治為基礎,因此有變更是正常的。 例如,2019 年 5 月 1 日之後,JapaneseCalendarJapaneseLunisolarCalendar 中將開始使用「令和」。 此變更對使用這些日曆的所有應用程式都有影響。 如需詳細資訊,以及判斷您的應用程式是否受到影響,請參閱 在 .NET 的日曆中處理新紀元。 如需在 Windows 系統上測試應用程式以確保其整備時間變更的相關資訊,請參閱 準備您的應用程式以進行日文紀元變更。 如需 .NET 中支援多個紀元的行事曆功能,以及使用支援多個紀元的行事歷時的最佳做法,請參閱 使用紀元

ParseExact(String, String, IFormatProvider)

來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs

使用指定的格式以及特定文化特性的格式資訊,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須完全符合指定的格式。

C#
public static DateTime ParseExact (string s, string format, IFormatProvider provider);
C#
public static DateTime ParseExact (string s, string format, IFormatProvider? provider);

參數

s
String

字串,包含要轉換的日期和時間。

format
String

格式規範,其定義 s 所需的格式。 如需詳細資訊,請參閱<備註>一節。

provider
IFormatProvider

提供關於 s 之特定文化特性格式資訊的物件。

傳回

物件,與 s 中包含的日期和時間相等,如 formatprovider 所指定。

例外狀況

sformatnull

sformat 為空字串。

-或-

s 不包含與 format 中所指定之模式對應的日期和時間。

-或-

小時元件和 s 中的 AM/PM 指示項不相符。

範例

下列範例示範 ParseExact 方法。

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string dateString, format;
      DateTime result;
      CultureInfo provider = CultureInfo.InvariantCulture;

      // Parse date-only value with invariant culture.
      dateString = "06/15/2008";
      format = "d";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      // Parse date-only value without leading zero in month using "d" format.
      // Should throw a FormatException because standard short date pattern of
      // invariant culture requires two-digit month.
      dateString = "6/15/2008";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      // Parse date and time with custom specifier.
      dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
      format = "ddd dd MMM yyyy h:mm tt zzz";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      // Parse date and time with offset but without offset's minutes.
      // Should throw a FormatException because "zzz" specifier requires leading
      // zero in hours.
      dateString = "Sun 15 Jun 2008 8:30 AM -06";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      dateString = "15/06/2008 08:30";
      format = "g";
      provider = new CultureInfo("fr-FR");
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }

      // Parse a date that includes seconds and milliseconds
      // by using the French (France) and invariant cultures.
      dateString = "18/08/2015 06:30:15.006542";
      format = "dd/MM/yyyy HH:mm:ss.ffffff";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", dateString);
      }
   }
}
// The example displays the following output:
//       06/15/2008 converts to 6/15/2008 12:00:00 AM.
//       6/15/2008 is not in the correct format.
//       Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
//       Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
//       15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
//       18/08/2015 06:30:15.006542 converts to 8/18/2015 6:30:15 AM.

備註

方法 DateTime.ParseExact(String, String, IFormatProvider) 會剖析日期的字串標記法,其格式必須是 參數所 format 定義的格式。 它也需要 < 日期和時間之字串表示的日期和時間 <>> 元素以 所 format 指定的順序顯示,而且 s 除了 允許 format 以外的空白字元。 如果 format 定義沒有時間元素且剖析作業成功的日期,則產生的 DateTime 值會在午夜 (00:00:00) 。 如果 format 定義沒有 date 元素且剖析作業成功的時間,則產生的 DateTime 值具有 的 DateTime.Now.Date 日期。

如果未 s 代表特定時區中的時間,且剖析作業成功, Kind 則傳 DateTime 回值的 屬性為 DateTimeKind.Unspecified 。 如果 s 確實代表特定時區的時間,並允許 format 時區資訊出現在 (,例如,如果 format 等於 「o」、「r」 或 「u」 標準格式規範,或如果它包含 「z」、「zz」 或 「zzz」 自訂格式規範) , Kind 則傳 DateTime 回值的 屬性為 DateTimeKind.Local

參數 format 是包含單一標準格式規範的字串,或定義 必要格式的 s 一或多個自訂格式規範。 如需有效格式化程式碼的詳細資訊,請參閱 標準日期和時間格式字串自訂日期和時間格式字串

備註

如果 format 是不包含日期或時間分隔符號的自訂格式模式, (例如 「yyyyMMMMddHmm」) ,請使用參數的不變異文化 provider 特性,以及每個自訂格式規範的最寬格式。 例如,如果您想要以格式模式指定小時,請指定較寬的格式 「HH」,而不是較窄的格式 「H」。

參數所定義的特定日期和時間符號和字串 (,例如特定語言 s 中星期幾的名稱,) 是由 參數所定義 provider ,而 如果 format 是標準格式規範字符串,則為 的精確格式 s 。 參數 provider 可以是下列任一項:

如果 providernull ,則會 CultureInfo 使用對應至目前文化特性的物件。

給呼叫者的注意事項

在 .NET Framework 4 中,如果要剖析的字串包含小時元件,以及未在合約中的 AM/PM 指示項, ParseExact 則方法會 FormatException 擲回 。 在 .NET Framework 3.5 和舊版中,會忽略 AM/PM 指示項。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs

使用指定的格式、特定文化特性格式資訊以及樣式,以將日期和時間的指定範圍表示轉換為與其相等的 DateTime。 字串表示的格式必須完全符合指定的格式,否則會擲回例外狀況。

C#
public static DateTime ParseExact (ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider? provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
C#
public static DateTime ParseExact (ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);

參數

s
ReadOnlySpan<Char>

包含字元的範圍,其表示要轉換的日期和時間。

format
ReadOnlySpan<Char>

包含字元的範圍,其表示定義 s 所需格式的格式規範。

provider
IFormatProvider

物件,其提供關於 s 的特定文化特性格式資訊。

style
DateTimeStyles

列舉值的位元組合,提供有關 s、可以出現在 s 中的樣式項目,或是從 s 轉換成 DateTime 值的詳細資訊。 一般會指定的值是 None

傳回

物件,與 s 中包含的日期和時間相等,如 formatproviderstyle 所指定。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)

來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs

使用指定格式、特定文化特性格式資訊以及樣式的陣列,以將日期和時間的指定範圍表示轉換為與其相等的 DateTime。 字串表示的格式必須至少完全符合其中一個指定的格式,否則會擲回例外狀況。

C#
public static DateTime ParseExact (ReadOnlySpan<char> s, string[] formats, IFormatProvider? provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
C#
public static DateTime ParseExact (ReadOnlySpan<char> s, string[] formats, IFormatProvider provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);

參數

s
ReadOnlySpan<Char>

包含字元的範圍,其表示要轉換的日期和時間。

formats
String[]

s 允許的格式陣列。

provider
IFormatProvider

提供關於 s 之特定文化特性格式資訊的物件。

style
DateTimeStyles

列舉值的位元組合,其表示 s 所允許的格式。 一般會指定的值是 None

傳回

物件,與 s 中包含的日期和時間相等,如 formatsproviderstyle 所指定。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

ParseExact(String, String, IFormatProvider, DateTimeStyles)

來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs

使用指定的格式、特定文化特性格式資訊以及樣式,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須完全符合指定的格式,否則會擲回例外狀況。

C#
public static DateTime ParseExact (string s, string format, IFormatProvider provider, System.Globalization.DateTimeStyles style);
C#
public static DateTime ParseExact (string s, string format, IFormatProvider? provider, System.Globalization.DateTimeStyles style);

參數

s
String

字串,含有要轉換的日期和時間。

format
String

格式規範,其定義 s 所需的格式。 如需詳細資訊,請參閱<備註>一節。

provider
IFormatProvider

物件,其提供關於 s 的特定文化特性格式資訊。

style
DateTimeStyles

列舉值的位元組合,提供有關 s、可以出現在 s 中的樣式項目,或是從 s 轉換成 DateTime 值的詳細資訊。 一般會指定的值是 None

傳回

物件,與 s 中包含的日期和時間相等,如 formatproviderstyle 所指定。

例外狀況

sformatnull

sformat 為空字串。

-或-

s 不包含與 format 中所指定之模式對應的日期和時間。

-或-

小時元件和 s 中的 AM/PM 指示項不相符。

style 包含 DateTimeStyles 值的無效組合。 如需範例,請參閱 AssumeLocalAssumeUniversal

範例

下列範例示範 ParseExact(String, String, IFormatProvider) 方法。 請注意,當參數等於 DateTimeStyles.Nonestyles ,無法成功剖析字串 「5/01/2009 8:30 AM」,因為 不允許前置空格。 format 此外,字串 「5/01/2009 09:00」 無法成功 format 剖析為 「MM/dd/yyyyhh:mm」 的字串,因為日期字串不會在月份編號前面加上前置零,因為需要。 format

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo enUS = new CultureInfo("en-US");
      string dateString;
      DateTime dateValue;

      // Parse date with no style flags.
      dateString = " 5/01/2009 8:30 AM";
      try {
         dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
      // Allow a leading space in the date string.
      try {
         dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }

      // Use custom formats with M and MM.
      dateString = "5/01/2009 09:00";
      try {
         dateValue = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
      // Allow a leading space in the date string.
      try {
         dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }

      // Parse a string with time zone information.
      dateString = "05/01/2009 01:30:42 PM -05:00";
      try {
         dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
      // Allow a leading space in the date string.
      try {
         dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS,
                                     DateTimeStyles.AdjustToUniversal);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }

      // Parse a string representing UTC.
      dateString = "2008-06-11T16:11:20.0904778Z";
      try {
         dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture,
                                     DateTimeStyles.None);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
      try {
         dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture,
                                     DateTimeStyles.RoundtripKind);
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                           dateValue.Kind);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      }
   }
}
// The example displays the following output:
//    ' 5/01/2009 8:30 AM' is not in an acceptable format.
//    Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
//    Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
//    '5/01/2009 09:00' is not in an acceptable format.
//    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
//    Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
//    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
//    Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).

備註

方法 DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) 會剖析日期的字串標記法,該字串表示必須是 參數所 format 定義的格式。 它也需要 中的 s 日期和時間元素以 所 format 指定的順序顯示。 如果 s 與 參數的 format 模式不相符,且參數所 style 定義的任何變化,方法會擲回 FormatException 。 相反地,方法會 DateTime.Parse(String, IFormatProvider, DateTimeStyles) 剖析格式提供者物件所辨識之任一格式中日期的 DateTimeFormatInfo 字串表示。 方法 DateTime.Parse(String, IFormatProvider, DateTimeStyles) 也允許 中的 s 日期和時間元素依任何順序顯示。

s如果參數只包含時間和沒有日期,除非參數包含 DateTimeStyles.NoCurrentDateDefault 旗標,否則 style 會使用目前的日期,在此情況下會使用預設日期 (DateTime.Date.MinValue) 。 s如果參數只包含日期且沒有時間,則會使用午夜 (00:00:00) 。 參數 style 也會決定參數是否可以 s 包含前置、內部或尾端空白字元。

如果 s 不包含時區資訊,則 KindDateTime 回物件的 屬性為 DateTimeKind.Unspecified 。 您可以使用 旗標來變更此行為,這個旗標會傳 DateTimeStyles.AssumeLocalDateTimeKind 屬性為 DateTimeKind.Local 的值,或是使用 DateTimeStyles.AssumeUniversalDateTimeStyles.AdjustToUniversal 旗標,傳回 DateTime 其屬性為 DateTimeKind.Utc 的值 Kind 。 如果 s 包含時區資訊,則會視需要將時間轉換成當地時間,且 KindDateTime 回物件的 屬性會設定為 DateTimeKind.Local 。 您可以使用 旗標將國際標準時間 (UTC) 轉換為當地時間,並將 屬性 DateTimeKind.Utc 設定 Kind 為 ,即可變更 DateTimeStyles.RoundtripKind 此行為。

參數 format 會定義參數的必要模式 s 。 它可以由 自訂日期和時間格式字串 資料表中的一或多個自訂格式規範所組成,或從 標準日期和時間格式字串 資料表識別預先定義的模式的單一標準格式規範。

如果您未在自訂格式模式中使用日期或時間分隔符號,請使用參數的不變異文化特性 provider ,以及每個自訂格式規範的最寬格式。 例如,如果您想要在模式中指定小時,請指定較寬的格式 「HH」,而不是較窄的表單 「H」。

備註

您可以呼叫 DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 方法,並指定多個允許的格式,而不需要 s 符合單一格式才能讓剖析作業成功。 這可讓剖析作業更可能成功。

參數 styles 包含列舉的 DateTimeStyles 一或多個成員,可判斷 和 未定義的 format 空白字元是否可以出現在 中 s ,以及控制剖析作業的精確行為。 下表描述列舉的每個成員 DateTimeStyles 如何影響方法的 ParseExact(String, String, IFormatProvider, DateTimeStyles) 作業。

DateTimeStyles 成員 描述
AdjustToUniversal 視需要剖析 s ,並將它轉換成 UTC。 如果 s 包含時區位移,或如果 s 不包含時區資訊,但 styles 包含 DateTimeStyles.AssumeLocal 旗標,方法會剖析字串、呼叫 ToUniversalTime 以將傳 DateTime 回的值轉換為 UTC,並將 屬性設定 KindDateTimeKind.Utc 。 如果 s 表示它代表 UTC,或者如果 s 不包含時區資訊,但 styles 包含 DateTimeStyles.AssumeUniversal 旗標,則 方法會剖析字串、在傳 DateTime 回的值上不執行時區轉換,並將 屬性設定 KindDateTimeKind.Utc 。 在其他所有情況下,旗標沒有任何作用。
AllowInnerWhite 指定 未定義的 format 空白字元可以出現在任何個別的日期或時間元素之間。
AllowLeadingWhite 指定 未定義的 format 空白字元可以出現在 的 s 開頭。
AllowTrailingWhite 指定 未定義的 format 空白字元可以出現在 的 s 結尾。
AllowWhiteSpaces 指定可能包含 s 未定義的前置、內部和尾端空白字元 format
AssumeLocal 指定如果 s 缺少任何時區資訊,則會假設它代表當地時間。 除非有 DateTimeStyles.AdjustToUniversal 旗標, Kind 否則傳 DateTime 回值的 屬性會設定為 DateTimeKind.Local
AssumeUniversal 指定如果 s 缺少任何時區資訊,則會假設它代表 UTC。 DateTimeStyles.AdjustToUniversal除非有 旗標,否則 方法會將傳 DateTime 回的值從 UTC 轉換為當地時間,並將其 屬性設定 KindDateTimeKind.Local
NoCurrentDateDefault 如果 s 包含沒有日期資訊的時間,則傳回值的日期會設定為 DateTime.MinValue.Date
None 參數 s 是使用預設值剖析。 不允許存在 以外的 format 空白字元。 如果 s 缺少日期元件,傳 DateTime 回值的日期會設定為 1/1/0001。 如果 s 不包含時區資訊,則 KindDateTime 回物件的 屬性會設定為 DateTimeKind.Unspecified 。 如果時區資訊存在於 中 s ,時間會轉換成當地時間,而 KindDateTime 回物件的 屬性會設定為 DateTimeKind.Local
RoundtripKind 對於包含時區資訊的字串,嘗試防止轉換成 DateTime 值日期和時間,並將其 Kind 屬性設定為 DateTimeKind.Local 。 此旗標主要會防止 UTC 時間轉換為當地時間。

參數會定義 provider 特定語言 (特定日期和時間符號和字串,例如,在 中使用 s 特定語言) 星期幾的名稱,就像 是標準格式規範字符串時的精確格式 sformat 一樣。 參數 provider 可以是下列任一項:

如果 為 providernull ,則會 CultureInfo 使用對應至目前文化特性的物件。

給呼叫者的注意事項

在 .NET Framework 4 中,如果要剖析的字串包含小時元件,以及未在合約中的 AM/PM 指示項, ParseExact 則方法會 FormatException 擲回 。 在 .NET Framework 3.5 和舊版中,會忽略 AM/PM 指示項。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ParseExact(String, String[], IFormatProvider, DateTimeStyles)

來源:
DateTime.cs
來源:
DateTime.cs
來源:
DateTime.cs

使用指定的格式陣列、特定文化特性格式資訊以及樣式,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須至少完全符合其中一個指定的格式,否則會擲回例外狀況。

C#
public static DateTime ParseExact (string s, string[] formats, IFormatProvider provider, System.Globalization.DateTimeStyles style);
C#
public static DateTime ParseExact (string s, string[] formats, IFormatProvider? provider, System.Globalization.DateTimeStyles style);

參數

s
String

字串,包含要轉換的日期和時間。

formats
String[]

s 允許的格式陣列。 如需詳細資訊,請參閱<備註>一節。

provider
IFormatProvider

提供關於 s 之特定文化特性格式資訊的物件。

style
DateTimeStyles

列舉值的位元組合,其表示 s 所允許的格式。 一般會指定的值是 None

傳回

物件,與 s 中包含的日期和時間相等,如 formatsproviderstyle 所指定。

例外狀況

sformatsnull

s 為空字串。

-或-

formats 的項目為空字串。

-或-

s 不包含對應 formats 任何項目的日期和時間。

-或-

小時元件和 s 中的 AM/PM 指示項不相符。

style 包含 DateTimeStyles 值的無效組合。 如需範例,請參閱 AssumeLocalAssumeUniversal

範例

下列範例會 DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 使用 方法來確保可以成功剖析一些可能格式的字串。

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
                         "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
                         "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
                         "M/d/yyyy h:mm", "M/d/yyyy h:mm",
                         "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
                         "MM/d/yyyy HH:mm:ss.ffffff" };
      string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM",
                              "5/1/2009 6:32:00", "05/01/2009 06:32",
                              "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00",
                              "08/28/2015 16:17:39.125", "08/28/2015 16:17:39.125000" };
      DateTime dateValue;

      foreach (string dateString in dateStrings)
      {
         try {
            dateValue = DateTime.ParseExact(dateString, formats,
                                            new CultureInfo("en-US"),
                                            DateTimeStyles.None);
            Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
         }
         catch (FormatException) {
            Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
//       Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
//       Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
//       Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
//       Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
//       Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
//       Unable to convert '08/28/2015 16:17:39.125' to a date.
//       Converted '08/28/2015 16:17:39.125000' to 8/28/2015 4:17:39 PM.

備註

方法 DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 會剖析日期的字串表示,該日期符合指派給 formats 參數的任何一個模式。 如果字串 s 與參數所 styles 定義的任何變化不符上述任何一個模式,則 方法會 FormatException 擲回 。 除了與多個格式模式比較 s ,而不是單一格式設定模式之外,此多載的行為與 DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) 方法相同。

參數 s 包含剖析的日期和時間。 s如果參數只包含時間和沒有日期,除非參數包含 DateTimeStyles.NoCurrentDateDefault 旗標,否則 style 會使用目前的日期,在此情況下會使用預設日期 (DateTime.Date.MinValue) 。 s如果參數只包含日期且沒有時間,則會使用午夜 (00:00:00) 。 參數 style 也會判斷 參數是否可以 s 包含 中其中一個格式字串 formats 所允許的前置、內部或尾端空白字元。

如果 s 不包含時區資訊,則 KindDateTime 回物件的 屬性為 DateTimeKind.Unspecified 。 您可以使用 旗標來變更此行為,這個旗標會傳 DateTimeStyles.AssumeLocalDateTimeKind 屬性為 DateTimeKind.Local 的值,或是使用 DateTimeStyles.AssumeUniversalDateTimeStyles.AdjustToUniversal 旗標,傳回 DateTime 其屬性為 DateTimeKind.Utc 的值 Kind 。 如果 s 包含時區資訊,則會視需要將時間轉換成當地時間,且 KindDateTime 回物件的 屬性會設定為 DateTimeKind.Local 。 您可以使用 旗標將國際標準時間 (UTC (UTC) 轉換為當地時間,並將 屬性設定 KindDateTimeKind.Utc ,藉此變更 DateTimeStyles.RoundtripKind 此行為。

參數 formats 包含模式陣列,其中 s 一個必須完全符合剖析作業成功。 參數中的 formats 模式是由 自訂日期和時間格式字串 資料表中的一或多個自訂格式規範所組成,或從 標準日期和時間格式字串 資料表識別預先定義的模式的單一標準格式規範。

如果您未在自訂格式模式中使用日期或時間分隔符號,請使用參數的不變異文化特性 provider ,以及每個自訂格式規範的最寬格式。 例如,如果您想要在模式中指定小時,請指定較寬的格式 「HH」,而不是較窄的表單 「H」。

參數 styles 包含列舉的 DateTimeStyles 一或多個成員,可判斷 和 未定義的 format 空白字元是否可以出現在 中 s ,以及控制剖析作業的精確行為。 下表描述列舉的每個成員 DateTimeStyles 如何影響方法的 ParseExact(String, String, IFormatProvider, DateTimeStyles) 作業。

DateTimeStyles 成員 描述
AdjustToUniversal 視需要剖析 s ,並將它轉換成 UTC。 如果 s 包含時區位移,或如果 s 不包含時區資訊,但 styles 包含 DateTimeStyles.AssumeLocal 旗標,方法會剖析字串、呼叫 ToUniversalTime 以將傳 DateTime 回的值轉換為 UTC,並將 屬性設定 KindDateTimeKind.Utc 。 如果 s 表示它代表 UTC,或者如果 s 不包含時區資訊,但 styles 包含 DateTimeStyles.AssumeUniversal 旗標,則 方法會剖析字串、在傳 DateTime 回的值上不執行時區轉換,並將 屬性設定 KindDateTimeKind.Utc 。 在其他所有情況下,旗標沒有任何作用。
AllowInnerWhite 指定 未定義的 format 空白字元可以出現在任何個別的日期或時間元素之間。
AllowLeadingWhite 指定 未定義的 format 空白字元可以出現在 的 s 開頭。
AllowTrailingWhite 指定 未定義的 format 空白字元可以出現在 的 s 結尾。
AllowWhiteSpaces 指定可能包含 s 未定義的前置、內部和尾端空白字元 format
AssumeLocal 指定如果 s 缺少任何時區資訊,則會假設它代表當地時間。 DateTimeStyles.AdjustToUniversal除非旗標存在, Kind 否則傳 DateTime 回值的 屬性會設定為 DateTimeKind.Local
AssumeUniversal 指定如果 s 缺少任何時區資訊,則會假設它代表 UTC。 DateTimeStyles.AdjustToUniversal除非旗標存在,否則方法會將傳 DateTime 回的值從 UTC 轉換為當地時間,並將其 屬性設定 KindDateTimeKind.Local
NoCurrentDateDefault 如果 s 包含不含日期資訊的時間,則傳回值的日期會設定為 DateTime.MinValue.Date
None 參數 s 會使用預設值進行剖析。 不允許存在 format 以外的空白字元。 如果 s 缺少日期元件,傳 DateTime 回值的日期會設定為 1/1/0001。 如果 s 不包含時區資訊, Kind 則傳 DateTime 回物件的 屬性會設定為 DateTimeKind.Unspecified 。 如果時區資訊存在於 中 s ,則時間會轉換成當地時間,而 KindDateTime 回物件的 屬性會設定為 DateTimeKind.Local
RoundtripKind 對於包含時區資訊的字串,嘗試防止將 屬性 Kind 設定 DateTimeKind.Local 為 的日期和時間轉換。 此旗標主要可防止 UTC 時間轉換為當地時間。

參數所定義的特定日期和時間符號和字串 (例如特定) s 語言中星期幾的名稱,由 參數定義 provider ,如同 標準格式規範字符串時的精確格式 sformat 。 參數 provider 可以是下列任一項:

如果 providernull ,則會 CultureInfo 使用對應至目前文化特性的物件。

給呼叫者的注意事項

在 .NET Framework 4 中 ParseExact ,如果要剖析的字串包含小時元件和未合約的 AM/PM 指示項,則方法會擲 FormatException 回 。 在 .NET Framework 3.5 和更早版本中,會忽略 AM/PM 指示項。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0