DateTime.TryParse 方法

定義

將日期和時間的指定字串表示,轉換為其相等的 DateTime,並傳回一個值表示轉換是否成功。

多載

TryParse(ReadOnlySpan<Char>, DateTime)

將日期和時間的指定字元範圍轉換為與其相等的 DateTime,並傳回一個值表示轉換是否成功。

TryParse(String, DateTime)

將日期和時間的指定字串表示,轉換為其相等的 DateTime,並傳回一個值表示轉換是否成功。

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime)

嘗試將字元範圍剖析成值。

TryParse(String, IFormatProvider, DateTime)

嘗試將字串剖析成值。

TryParse(String, IFormatProvider, DateTimeStyles, DateTime)

使用指定的特定文化特性格式資訊和格式樣式,將日期和時間的指定字串表示轉換為其相等的 DateTime,並傳回值,這個值表示轉換是否成功。

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)

使用指定的特定文化特性格式資訊和格式樣式,以將日期和時間的範圍表示轉換為與其相等的 DateTime,並傳回一個值表示轉換是否成功。

備註

重要

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

TryParse(ReadOnlySpan<Char>, DateTime)

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

將日期和時間的指定字元範圍轉換為與其相等的 DateTime,並傳回一個值表示轉換是否成功。

C#
public static bool TryParse(ReadOnlySpan<char> s, out DateTime result);

參數

s
ReadOnlySpan<Char>

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

result
DateTime

當這個方法傳回時,如果轉換成功,則包含 DateTime 相當於 中 s所含日期和時間的值,如果轉換失敗,則包含 DateTime.MinValue 。 如果 s 參數為 null、為空字串 ("") 或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。

傳回

如果 true 參數轉換成功,則為 s,否則為 false

適用於

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

TryParse(String, DateTime)

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

將日期和時間的指定字串表示,轉換為其相等的 DateTime,並傳回一個值表示轉換是否成功。

C#
public static bool TryParse(string s, out DateTime result);
C#
public static bool TryParse(string? s, out DateTime result);

參數

s
String

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

result
DateTime

當這個方法傳回時,如果轉換成功,則包含 DateTime 相當於 中 s所含日期和時間的值,如果轉換失敗,則包含 DateTime.MinValue 。 如果 s 參數為 null、為空字串 ("") 或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。

傳回

如果 true 參數轉換成功,則為 s,否則為 false

範例

下列範例會將一些日期和時間字串 DateTime.TryParse(String, DateTime) 傳遞給方法。

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] dateStrings = {"05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8",
                              "2009-05-01T14:57:32.8375298-04:00", "5/01/2008",
                              "5/01/2008 14:57:32.80 -07:00",
                              "1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM",
                              "Fri, 15 May 2009 20:10:57 GMT" };
      DateTime dateValue;

      Console.WriteLine("Attempting to parse strings using {0} culture.",
                        CultureInfo.CurrentCulture.Name);
      foreach (string dateString in dateStrings)
      {
         if (DateTime.TryParse(dateString, out dateValue))
            Console.WriteLine("  Converted '{0}' to {1} ({2}).", dateString,
                              dateValue, dateValue.Kind);
         else
            Console.WriteLine("  Unable to parse '{0}'.", dateString);
      }
   }
}
// The example displays output like the following:
//    Attempting to parse strings using en-US culture.
//      Converted '05/01/2009 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
//      Converted '2009-05-01 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
//      Converted '2009-05-01T14:57:32.8375298-04:00' to 5/1/2009 11:57:32 AM (Local).
//
//      Converted '5/01/2008' to 5/1/2008 12:00:00 AM (Unspecified).
//      Converted '5/01/2008 14:57:32.80 -07:00' to 5/1/2008 2:57:32 PM (Local).
//      Converted '1 May 2008 2:57:32.8 PM' to 5/1/2008 2:57:32 PM (Unspecified).
//      Unable to parse '16-05-2009 1:00:32 PM'.
//      Converted 'Fri, 15 May 2009 20:10:57 GMT' to 5/15/2009 1:10:57 PM (Local).

備註

方法 DateTime.TryParse(String, DateTime) 與 方法類似 DateTime.Parse(String) ,不同之處在於 TryParse(String, DateTime) 方法在轉換失敗時不會擲回例外狀況。

字串 s 會使用目前 物件中的格式設定資訊進行剖析,此資訊是由目前 DateTimeFormatInfo 文化特性隱含提供。

如果可能的話,此方法會嘗試忽略無法辨識的數據,並以目前日期填入遺漏的月份、日和年份資訊。 如果 s 只包含日期且沒有時間,這個方法會假設時間是午夜 12:00。 如果 s 包含具有兩位數年份的日期元件,則會根據 屬性的值 Calendar.TwoDigitYearMax ,轉換成目前文化特性目前行事曆中的年份。 中 s 任何前置、內部或尾端空格符都會被忽略。 日期和時間可以以一對開頭和尾端的 NUMBER SIGN 字元括住, ('#'、U+0023) ,而且可以以一或多個 NULL 字符結尾 (U+0000) 。

DateTime.TryParse(String, DateTime)因為方法會嘗試使用目前文化特性的格式規則來剖析日期和時間的字串表示,所以嘗試跨不同文化特性剖析特定字串可能會失敗或傳回不同的結果。 如果特定日期和時間格式將在不同的地區設定之間剖析,請使用 DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) 方法或方法的其中一個多載 TryParseExact ,並提供格式規範。

如果 s 是目前行事曆中閏年的閏日字串表示法,則方法會成功剖析 s 。 如果 s 是目前文化特性目前行事曆中非閏年的閏日字串表示法,則剖析作業會失敗,而且方法會傳 false回 。

如果 s 不包含時區資訊,result則包含DateTime其 屬性為 DateTimeKind.Unspecified 方法傳回時的值Kind。 如果要剖析的字串包含時區資訊, result 則包含 DateTime 屬性值 KindDateTimeKind.Local 方法傳回時的值。

給呼叫者的注意事項

格式設定會受到目前 DateTimeFormatInfo 對象的屬性所影響,根據預設,這些屬性衍生自 [控制面板] 中的 [ 地區和語言選項 ] 專案。 如果TryParse目前的 DateSeparator 與屬性設定為相同的值,方法可能會意外失敗並TimeSeparator傳回 False

另請參閱

適用於

.NET 10 及其他版本
產品 版本
.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, 10
.NET Framework 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

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime)

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

嘗試將字元範圍剖析成值。

C#
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out DateTime result);

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

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

result
DateTime

當這個方法傳回時,會包含成功剖析 s的結果,或失敗時未定義的值。

傳回

true 如果 s 已成功剖析,則為 ,否則為 false

適用於

.NET 10 及其他版本
產品 版本
.NET 7, 8, 9, 10

TryParse(String, IFormatProvider, DateTime)

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

嘗試將字串剖析成值。

C#
public static bool TryParse(string? s, IFormatProvider? provider, out DateTime result);

參數

s
String

要剖析的字串。

provider
IFormatProvider

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

result
DateTime

當這個方法傳回時,包含成功剖析 s 或失敗時未定義值的結果。

傳回

true 如果 s 已成功剖析,則為 ,否則為 false

適用於

.NET 10 及其他版本
產品 版本
.NET 7, 8, 9, 10

TryParse(String, IFormatProvider, DateTimeStyles, DateTime)

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

使用指定的特定文化特性格式資訊和格式樣式,將日期和時間的指定字串表示轉換為其相等的 DateTime,並傳回值,這個值表示轉換是否成功。

C#
public static bool TryParse(string s, IFormatProvider provider, System.Globalization.DateTimeStyles styles, out DateTime result);
C#
public static bool TryParse(string? s, IFormatProvider? provider, System.Globalization.DateTimeStyles styles, out DateTime result);

參數

s
String

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

provider
IFormatProvider

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

styles
DateTimeStyles

列舉值的位元組合,這個組合會定義如何根據目前時區或目前日期解譯已剖析的日期。 一般會指定的值是 None

result
DateTime

當這個方法傳回時,如果轉換成功,則包含 DateTime 相當於 中 s所含日期和時間的值,如果轉換失敗,則包含 DateTime.MinValue 。 如果 s 參數為 null、為空字串 ("") 或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。

傳回

如果 true 參數轉換成功,則為 s,否則為 false

例外狀況

styles 不是有效的 DateTimeStyles 值。

-或-

styles 包含 DateTimeStyles 值的無效組合 (例如,AssumeLocalAssumeUniversal)。

provider 是中性文化特性,不能用在剖析作業。

範例

下列範例說明 DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) 方法。

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string dateString;
      CultureInfo culture;
      DateTimeStyles styles;
      DateTime dateResult;

      // Parse a date and time with no styles.
      dateString = "03/01/2009 10:00 AM";
      culture = CultureInfo.CreateSpecificCulture("en-US");
      styles = DateTimeStyles.None;
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.",
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.",
                           dateString);

      // Parse the same date and time with the AssumeLocal style.
      styles = DateTimeStyles.AssumeLocal;
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.",
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString);

      // Parse a date and time that is assumed to be local.
      // This time is five hours behind UTC. The local system's time zone is
      // eight hours behind UTC.
      dateString = "2009/03/01T10:00:00-5:00";
      styles = DateTimeStyles.AssumeLocal;
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.",
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString);

      // Attempt to convert a string in improper ISO 8601 format.
      dateString = "03/01/2009T10:00:00-5:00";
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.",
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString);

      // Assume a date and time string formatted for the fr-FR culture is the local
      // time and convert it to UTC.
      dateString = "2008-03-01 10:00";
      culture = CultureInfo.CreateSpecificCulture("fr-FR");
      styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal;
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.",
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
   }
}
// The example displays the following output to the console:
//       03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Unspecified.
//       03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Local.
//       2009/03/01T10:00:00-5:00 converted to 3/1/2009 7:00:00 AM Local.
//       Unable to convert 03/01/2009T10:00:00-5:00 to a date and time.
//       2008-03-01 10:00 converted to 3/1/2008 6:00:00 PM Utc.

備註

如需此 API 的詳細資訊,請參閱 DateTime.TryParse 的補充 API 備註

給呼叫者的注意事項

格式會受到目前 DateTimeFormatInfo 對象的屬性所影響,由 參數提供 provider 。 如果TryParse目前的 DateSeparator 與屬性設定為相同的值,方法可能會意外失敗並TimeSeparator傳回 False

另請參閱

適用於

.NET 10 及其他版本
產品 版本
.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, 10
.NET Framework 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

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)

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

使用指定的特定文化特性格式資訊和格式樣式,以將日期和時間的範圍表示轉換為與其相等的 DateTime,並傳回一個值表示轉換是否成功。

C#
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, System.Globalization.DateTimeStyles styles, out DateTime result);
C#
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, System.Globalization.DateTimeStyles styles, out DateTime result);

參數

s
ReadOnlySpan<Char>

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

provider
IFormatProvider

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

styles
DateTimeStyles

列舉值的位元組合,這個組合會定義如何根據目前時區或目前日期解譯已剖析的日期。 一般會指定的值是 None

result
DateTime

當這個方法傳回時,如果轉換成功,則包含 DateTime 相當於 中 s所含日期和時間的值,如果轉換失敗,則包含 DateTime.MinValue 。 如果 s 參數為 null、為空字串 ("") 或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。

傳回

如果 true 參數轉換成功,則為 s,否則為 false

適用於

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