DateTimeOffset.TryParseExact 方法

定義

將日期和時間的指定字串表示,轉換為其相等的 DateTimeOffset。 字串表示的格式必須完全符合指定的格式。

多載

TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)

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

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset)

使用指定的格式、文化特性特定格式資訊與樣式,將字元範圍內的日期和時間表示,轉換為其相等的 DateTimeOffset。 日期和時間表示的格式必須完全符合指定的格式。

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

使用指定的格式、特定的文化特性格式資訊及樣式,將字元範圍中的日期和時間表示轉換為其對等的 DateTimeOffset。 日期和時間表示的格式必須完全符合其中一個指定的格式。

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset)

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

TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

public:
 static bool TryParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (string? input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : string * string[] * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

參數

input
String

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

formats
String[]

陣列,定義 input 的預期格式。

formatProvider
IFormatProvider

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

styles
DateTimeStyles

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

result
DateTimeOffset

當方法傳回時,如果轉換成功,則包含 相當於 的日期和時間 input ,如果轉換失敗,則為 DateTimeOffsetDateTimeOffset.MinValue。 如果 input 不包含日期和時間的有效字串表示,或者不包含 format 定義之預期格式的日期和時間,或者 formatsnull,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。

傳回

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

例外狀況

styles 包含未定義的 DateTimeStyles 值。

-或-

不支援 NoCurrentDateDefault

-或-

styles 包含互斥的 DateTimeStyles 值。

範例

下列範例會針對日期和時間和位移值的字串表示定義多個輸入格式,然後將使用者輸入的字串傳遞至 TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset) 方法。

TextReader conIn = Console.In;
TextWriter conOut = Console.Out;
int tries = 0;
string input = String.Empty;

string[] formats = new string[] {"M/dd/yyyy HH:m zzz", "MM/dd/yyyy HH:m zzz",
                                 "M/d/yyyy HH:m zzz", "MM/d/yyyy HH:m zzz",
                                 "M/dd/yy HH:m zzz", "MM/dd/yy HH:m zzz",
                                 "M/d/yy HH:m zzz", "MM/d/yy HH:m zzz",
                                 "M/dd/yyyy H:m zzz", "MM/dd/yyyy H:m zzz",
                                 "M/d/yyyy H:m zzz", "MM/d/yyyy H:m zzz",
                                 "M/dd/yy H:m zzz", "MM/dd/yy H:m zzz",
                                 "M/d/yy H:m zzz", "MM/d/yy H:m zzz",
                                 "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz",
                                 "M/d/yyyy HH:mm zzz", "MM/d/yyyy HH:mm zzz",
                                 "M/dd/yy HH:mm zzz", "MM/dd/yy HH:mm zzz",
                                 "M/d/yy HH:mm zzz", "MM/d/yy HH:mm zzz",
                                 "M/dd/yyyy H:mm zzz", "MM/dd/yyyy H:mm zzz",
                                 "M/d/yyyy H:mm zzz", "MM/d/yyyy H:mm zzz",
                                 "M/dd/yy H:mm zzz", "MM/dd/yy H:mm zzz",
                                 "M/d/yy H:mm zzz", "MM/d/yy H:mm zzz"};
IFormatProvider provider = CultureInfo.InvariantCulture.DateTimeFormat;
DateTimeOffset result;

do {
   conOut.WriteLine("Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),");
   conOut.Write("Then press Enter: ");
   input = conIn.ReadLine();
   conOut.WriteLine();
   if (DateTimeOffset.TryParseExact(input, formats, provider,
                                   DateTimeStyles.AllowWhiteSpaces,
                                   out result))
   {
      break;
   }
   else
   {
      Console.WriteLine("Unable to parse {0}.", input);
      tries++;
   }
} while (tries < 3);
if (tries >= 3)
   Console.WriteLine("Exiting application without parsing {0}", input);
else
   Console.WriteLine("{0} was converted to {1}", input, result.ToString());
// Some successful sample interactions with the user might appear as follows:
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/08/2007 6:54 -6:00
//
//    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/8/2007 06:54 -06:00
//
//    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/5/07 6:54 -6:00
//
//    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00
let mutable result = None    
let mutable tries = 0
let mutable input = ""

let formats = 
    [| "M/dd/yyyy HH:m zzz"; "MM/dd/yyyy HH:m zzz"
       "M/d/yyyy HH:m zzz"; "MM/d/yyyy HH:m zzz"
       "M/dd/yy HH:m zzz"; "MM/dd/yy HH:m zzz"
       "M/d/yy HH:m zzz"; "MM/d/yy HH:m zzz"
       "M/dd/yyyy H:m zzz"; "MM/dd/yyyy H:m zzz"
       "M/d/yyyy H:m zzz"; "MM/d/yyyy H:m zzz"
       "M/dd/yy H:m zzz"; "MM/dd/yy H:m zzz"
       "M/d/yy H:m zzz"; "MM/d/yy H:m zzz"
       "M/dd/yyyy HH:mm zzz"; "MM/dd/yyyy HH:mm zzz"
       "M/d/yyyy HH:mm zzz"; "MM/d/yyyy HH:mm zzz"
       "M/dd/yy HH:mm zzz"; "MM/dd/yy HH:mm zzz"
       "M/d/yy HH:mm zzz"; "MM/d/yy HH:mm zzz"
       "M/dd/yyyy H:mm zzz"; "MM/dd/yyyy H:mm zzz"
       "M/d/yyyy H:mm zzz"; "MM/d/yyyy H:mm zzz"
       "M/dd/yy H:mm zzz"; "MM/dd/yy H:mm zzz"
       "M/d/yy H:mm zzz"; "MM/d/yy H:mm zzz" |]
let provider = CultureInfo.InvariantCulture.DateTimeFormat

while tries < 3 && result.IsNone do
    printfn "Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),"
    printf "Then press Enter: "
    input <- stdin.ReadLine()
    printfn ""
    match DateTimeOffset.TryParseExact(input, formats, provider, DateTimeStyles.AllowWhiteSpaces) with
    | true, dto ->
        result <- Some dto
    | _ ->
        printfn $"Unable to parse {input}."
    tries <- tries + 1

match result with
| Some result ->
    printfn $"{input} was converted to {result}"
| None ->
    printfn $"Exiting application without parsing {input}"

// Some successful sample interactions with the user might appear as follows:
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/08/2007 6:54 -6:00
//
//    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/8/2007 06:54 -06:00
//
//    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/5/07 6:54 -6:00
//
//    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00
 Dim conIn As TextReader = Console.In
 Dim conOut As TextWriter = Console.Out
 Dim tries As Integer = 0
 Dim input As String = String.Empty
 Dim formats() As String = {"M/dd/yyyy HH:m zzz", "MM/dd/yyyy HH:m zzz", _
                            "M/d/yyyy HH:m zzz", "MM/d/yyyy HH:m zzz", _
                            "M/dd/yy HH:m zzz", "MM/dd/yy HH:m zzz", _
                            "M/d/yy HH:m zzz", "MM/d/yy HH:m zzz", _                                 
                            "M/dd/yyyy H:m zzz", "MM/dd/yyyy H:m zzz", _
                            "M/d/yyyy H:m zzz", "MM/d/yyyy H:m zzz", _
                            "M/dd/yy H:m zzz", "MM/dd/yy H:m zzz", _
                            "M/d/yy H:m zzz", "MM/d/yy H:m zzz", _                               
                            "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz", _
                            "M/d/yyyy HH:mm zzz", "MM/d/yyyy HH:mm zzz", _
                            "M/dd/yy HH:mm zzz", "MM/dd/yy HH:mm zzz", _
                            "M/d/yy HH:mm zzz", "MM/d/yy HH:mm zzz", _                                 
                            "M/dd/yyyy H:mm zzz", "MM/dd/yyyy H:mm zzz", _
                            "M/d/yyyy H:mm zzz", "MM/d/yyyy H:mm zzz", _
                            "M/dd/yy H:mm zzz", "MM/dd/yy H:mm zzz", _
                            "M/d/yy H:mm zzz", "MM/d/yy H:mm zzz"}   
 Dim provider As IFormatProvider = CultureInfo.InvariantCulture.DateTimeFormat
 Dim result As DateTimeOffset

 Do 
    conOut.WriteLine("Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),")
    conOut.Write("Then press Enter: ")
    input = conIn.ReadLine()
    conOut.WriteLine() 
    If DateTimeOffset.TryParseExact(input, formats, provider, _
                                    DateTimeStyles.AllowWhiteSpaces, _
                                    result) Then
       Exit Do
    Else
       Console.WriteLine("Unable to parse {0}.", input)      
       tries += 1
    End If
 Loop While tries < 3
 If tries >= 3 Then
    Console.WriteLine("Exiting application without parsing {0}", input)
 Else
    Console.WriteLine("{0} was converted to {1}", input, result.ToString())                                                     
 End If 
 ' Some successful sample interactions with the user might appear as follows:
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/08/2007 6:54 -6:00
 '    
 '    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00         
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/8/2007 06:54 -06:00
 '    
 '    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/5/07 6:54 -6:00
 '    
 '    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00

備註

方法 TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset) 會剖析日期的字串表示,其符合指派給 formats 陣列的任何一個模式。 input如果字串與參數所 styles 定義的任何變化不相符,則剖析作業會失敗,而且方法會傳 false 回 。 除了比較 input 包含格式規範的多個字串之外,這個多載的行為與 方法相同 DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles)

參數 formats 是字串陣列,其元素包含單一標準格式規範或一或多個自訂格式規範,以定義 可能的模式 input 。 如需有效格式化程式碼的詳細資訊,請參閱 標準日期和時間格式字串自訂日期和時間格式字串。 如果 中的 formats 相符專案包含 zzzzzz 自訂格式規範,表示位移必須存在於 input 中,該位移必須包含負號或正負號。 如果遺漏符號,剖析作業會失敗,而且方法會傳 false 回 。

重要

formats使用此多載的 參數來指定多個格式,有助於減少許多使用者在輸入日期和時間時遇到的挫折。 特別是,定義多個輸入模式的能力可讓應用程式處理日期和時程表示法,這些標記法可以包含或缺少月份、天數、小時、分鐘和秒的前置零。 此範例提供此範例的圖例。

如果 中的 formats 相符專案需要 input 包含日期而非時間,則產生的 DateTimeOffset 物件會指派午夜 (0:00:00) 的時間。 如果 中的 formats 相符專案需要該輸入包含時間,但不包含日期,則產生的 DateTimeOffset 物件會指派本機系統上的目前日期。 如果 中的 formats 相符專案不需要包含 input 位移,則產生的 DateTimeOffset 物件位移取決於 參數的值 styles 。 如果 styles 包含 AssumeLocal ,則會將本機時區的位移指派給 DateTimeOffset 物件。 如果 styles 包含 AssumeUniversal ,則會將國際標準時間 (UTC) 位移或 +00:00 指派給 DateTimeOffset 物件。 如果未指定任何值,則會使用當地時區的位移。

input 所使用的特定日期和時間符號和字串是由 參數所 formatProvider 定義。 如果 的 formats 相符專案是標準格式規範字符串,則的精確模式 input 也是如此。 參數 formatProvider 可以是下列其中一項:

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

參數 styles 會定義輸入字串中是否允許空白字元、指出剖析沒有明確位移元件的字串如何剖析,並支援在剖析作業期間進行 UTC 轉換。 除了 之外 NoCurrentDateDefault ,支援列舉的所有成員 DateTimeStyles 。 下表列出每個支援成員的效果。

DateTimeStyles 成員 行為
AdjustToUniversal input視需要剖析 ,並將它轉換成 UTC。 它相當於剖析字串,然後呼叫 DateTimeOffset.ToUniversalTimeDateTimeOffset 回物件的 方法。
AssumeLocal 如果 中的 formats 相符專案不需要 input 包含位移值,則傳 DateTimeOffset 回的物件會獲得當地時區的位移。 這是預設值。
AssumeUniversal 如果 中的 formats 相符專案不需要 input 包含位移值,則會將傳 DateTimeOffset 回的物件指定為 UTC 位移 (+00:00) 。
AllowInnerWhite 允許 input 包含 中 formats 專案未指定的內部空白字元。 除了位移 () 之外,在日期和時間元件之間和個別元件之間,可能會顯示額外的空白字元,並在剖析字串時忽略。
AllowLeadingWhite 允許 input 包含 中 formats 專案未指定的前置空格。 剖析字串時會忽略這些。
AllowTrailingWhite 允許 input 包含 中 formats 專案未指定的尾端空格。 剖析字串時會忽略這些。
AllowWhiteSpaces 允許 input 包含 中 formats 專案未指定的前置、尾端和內部空格。 剖析字串時,會忽略 中 formats 相符專案未指定的所有額外空白字元。
None 表示 中 input 不允許額外的空白字元。 空白字元必須與 中的特定元素 formats 完全相同,才能成功比對。 此為預設行為。
RoundtripKind 沒有作用, DateTimeOffset 因為 結構不包含 Kind 屬性。

給呼叫者的注意事項

在 .NET Framework 4 中,如果要剖析的字串包含小時元件,以及未同意的 AM/PM 指示項,則會 TryParseExact 傳回 false 。 在 .NET Framework 3.5 和舊版中,會忽略 AM/PM 指示項。

適用於

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定的格式、文化特性特定格式資訊與樣式,將字元範圍內的日期和時間表示,轉換為其相等的 DateTimeOffset。 日期和時間表示的格式必須完全符合指定的格式。

public:
 static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

參數

input
ReadOnlySpan<Char>

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

format
ReadOnlySpan<Char>

格式規範,其定義 input 所需的格式。

formatProvider
IFormatProvider

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

styles
DateTimeStyles

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

result
DateTimeOffset

當方法傳回時,如果轉換成功,則包含 DateTimeOffset 相當於 的日期和時間 input ,如果轉換失敗,則為 DateTimeOffset.MinValue 。 若...,轉換會失敗。

傳回

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

例外狀況

styles 包含未定義的 DateTimeStyles 值。 -或- 不支援 NoCurrentDateDefault。 -或- styles 包括互斥的 DateTimeStyles 值。

備註

這個多載就像 DateTimeOffset.ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) 方法一樣,不同之處在于,如果轉換失敗,此方法不會擲回例外狀況。 它會剖析日期和時間的標記法,該標記法必須與 參數所 format 指定的模式完全相符。 如果 input 不符合此模式,且參數所 styles 定義的空白字元中有一些可能的變化,則剖析作業會失敗,而且方法會傳 false 回 。

參數 format 是包含單一標準格式規範或一或多個自訂格式規範的字元範圍,可定義 的必要模式 input 。 如需有效格式化程式碼的詳細資訊,請參閱 標準日期和時間格式字串自訂日期和時間格式字串。 如果 format 包含 zzzzzz 自訂格式規範,表示位移必須存在於 input 中,該位移必須包含負號或正負號。 如果遺漏符號,剖析作業會失敗,而且方法會傳 false 回 。

如果需要 format 包含 input 日期而非時間,則產生的 DateTimeOffset 物件會指派午夜 (0:00:00) 的時間。 如果需要 format 包含 input 時間,但不包含日期,產生的 DateTimeOffset 物件會指派本機系統上的目前日期。 如果 format 不需要包含 input 位移,則產生的 DateTimeOffset 物件位移取決於 參數的值 styles 。 如果 styles 包含 AssumeLocal ,則會將本機時區的位移指派給 DateTimeOffset 物件。 如果 styles 包含 AssumeUniversal ,則會將國際標準時間 (UTC) 位移或 +00:00 指派給 DateTimeOffset 物件。 如果未指定任何值,則會使用當地時區的位移。

input 所使用的特定日期和時間符號和字串是由 參數所 formatProvider 定義。 如果 是標準格式規範字符串,則的精確模式 inputformat 也是如此。 參數 formatProvider 可以是下列其中一項:

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

參數 styles 會定義輸入字串中是否允許空白字元、指出如何剖析不含明確位移元件的字串,並支援在剖析作業期間進行 UTC 轉換。 除了 之外 NoCurrentDateDefault ,支援列舉的所有成員 DateTimeStyles 。 下表列出每個支援成員的效果。

DateTimeStyles 成員 行為
AdjustToUniversal input視需要剖析 ,並將它轉換成 UTC。 它相當於剖析日期和時程表示法,然後呼叫 DateTimeOffset.ToUniversalTimeDateTimeOffset 回物件的 方法。
AssumeLocal 如果 format 不需要包含 input 位移值,則傳 DateTimeOffset 回的物件會獲得當地時區的位移。 此為預設行為。
AssumeUniversal 如果 format 不需要包含 input 位移值,則會將傳 DateTimeOffset 回的物件指定為 UTC 位移 (+00:00) 。
AllowInnerWhite 允許 input 包含格式未指定的內部空白字元。 在日期和時間元件與個別元件之間,除了位移之外,也會在剖析字串時忽略額外的空白字元。
AllowLeadingWhite 允許 input 包含 未指定的 format 前置空格。 剖析字串時會忽略這些。
AllowTrailingWhite 允許 input 包含 未指定的 format 尾端空格。 剖析字串時會忽略這些。
AllowWhiteSpaces 允許 input 包含開頭、尾端和未指定的 format 內部空格。 剖析字串時,不會忽略 中 format 未指定的所有額外空白字元。
None 表示 中 input 不允許額外的空白字元。 空白字元必須與 中指定的 format 完全相同。 此為預設行為。
RoundtripKind 沒有作用,因為 DateTimeOffset 結構不包含 Kind 屬性。

適用於

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

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

使用指定的格式、特定的文化特性格式資訊及樣式,將字元範圍中的日期和時間表示轉換為其對等的 DateTimeOffset。 日期和時間表示的格式必須完全符合其中一個指定的格式。

public:
 static bool TryParseExact(ReadOnlySpan<char> input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

參數

input
ReadOnlySpan<Char>

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

formats
String[]

標準或自訂格式字串的陣列,這些格式字串定義可接受的 input 格式。

formatProvider
IFormatProvider

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

styles
DateTimeStyles

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

result
DateTimeOffset

當方法傳回時,如果轉換成功,則包含 DateTimeOffset 相當於 的日期和時間 input ,如果轉換失敗,則為 DateTimeOffset.MinValue 。 若...,轉換會失敗。

傳回

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

例外狀況

styles 包含未定義的 DateTimeStyles 值。 -或- 不支援 NoCurrentDateDefault。 -或- styles 包括互斥的 DateTimeStyles 值。

備註

這個方法會剖析日期的字串表示,其符合指派給 formats 陣列的任一模式。 如果 input 與 參數所 styles 定義的任何變化不相符,剖析作業會失敗,而且方法會傳 false 回 。 除了比較 input 包含格式規範的多個字串之外,這個多載的行為與 方法相同 DateTimeOffset.ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)

參數 formats 是字串陣列,其元素包含單一標準格式規範或一或多個自訂格式規範,以定義 可能的模式 input 。 如需有效格式化程式碼的詳細資訊,請參閱 標準日期和時間格式字串自訂日期和時間格式字串。 如果 中的 formats 相符專案包含 zzzzzz 自訂格式規範,表示位移必須存在於 input 中,該位移必須包含負號或正負號。 如果遺漏符號,剖析作業會失敗,而且方法會傳 false 回 。

重要

formats使用此多載的 參數來指定多個格式,有助於減少許多使用者在輸入日期和時間時遇到的挫折。 特別是,定義多個輸入模式的能力可讓應用程式處理日期和時程表示法,這些標記法可以包含或缺少月份、天數、小時、分鐘和秒的前置零。 此範例提供此範例的圖例。

如果 中的 formats 相符專案需要 input 包含日期而非時間,則產生的 DateTimeOffset 物件會指派午夜 (0:00:00) 的時間。 如果 中的 formats 相符專案需要該輸入包含時間,但不包含日期,則產生的 DateTimeOffset 物件會指派本機系統上的目前日期。 如果 中的 formats 相符專案不需要包含 input 位移,則產生的 DateTimeOffset 物件位移取決於 參數的值 styles 。 如果 styles 包含 DateTimeStyles.AssumeLocal ,則會將本機時區的位移指派給 DateTimeOffset 物件。 如果 styles 包含 DateTimeStyles.AssumeUniversal ,則會將國際標準時間 (UTC) 位移或 +00:00 指派給 DateTimeOffset 物件。 如果未指定任何值,則會使用當地時區的位移。

input 所使用的特定日期和時間符號是由 參數所 formatProvider 定義。 如果 的 formats 相符專案是標準格式規範字符串,則的精確模式 input 也是如此。 參數 formatProvider 可以是下列其中一項:

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

參數 styles 會定義輸入字串中是否允許空白字元、指出剖析沒有明確位移元件的字串如何剖析,並支援在剖析作業期間進行 UTC 轉換。 除了 之外 NoCurrentDateDefault ,支援列舉的所有成員 DateTimeStyles 。 下表列出每個支援成員的效果。

DateTimeStyles 成員 行為
AdjustToUniversal input視需要剖析 ,並將它轉換成 UTC。 它相當於剖析字串,然後呼叫 DateTimeOffset.ToUniversalTimeDateTimeOffset 回物件的 方法。
AssumeLocal 如果 中的 formats 相符專案不需要 input 包含位移值,則傳 DateTimeOffset 回的物件會獲得當地時區的位移。 這是預設值。
AssumeUniversal 如果 中的 formats 相符專案不需要 input 包含位移值,則會將傳 DateTimeOffset 回的物件指定為 UTC 位移 (+00:00) 。
AllowInnerWhite 允許 input 包含 中 formats 專案未指定的內部空白字元。 除了位移 () 之外,在日期和時間元件之間和個別元件之間,可能會顯示額外的空白字元,並在剖析字串時忽略。
AllowLeadingWhite 允許 input 包含 中 formats 專案未指定的前置空格。 剖析字串時會忽略這些。
AllowTrailingWhite 允許 input 包含 中 formats 專案未指定的尾端空格。 剖析字串時會忽略這些。
AllowWhiteSpaces 允許 input 包含 中 formats 專案未指定的前置、尾端和內部空格。 剖析字串時,會忽略 中 formats 相符專案未指定的所有額外空白字元。
None 表示 中 input 不允許額外的空白字元。 空白字元必須與 中的特定元素 formats 完全相同,才能成功比對。 此為預設行為。
RoundtripKind 沒有作用, DateTimeOffset 因為 結構不包含 Kind 屬性。

適用於

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

public:
 static bool TryParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (string input, string format, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (string? input, string? format, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : string * string * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

參數

input
String

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

format
String

格式規範,其定義 input 所需的格式。

formatProvider
IFormatProvider

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

styles
DateTimeStyles

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

result
DateTimeOffset

當方法傳回時,如果轉換成功,則包含 相當於 的日期和時間 input ,如果轉換失敗,則為 DateTimeOffsetDateTimeOffset.MinValue。 如果 input 參數為 null,或者不包含由 formatprovider定義的預期格式之日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。

傳回

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

例外狀況

styles 包含未定義的 DateTimeStyles 值。

-或-

不支援 NoCurrentDateDefault

-或-

styles 包含互斥的 DateTimeStyles 值。

範例

下列範例會 TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset) 使用 方法搭配標準和自訂格式規範、不變異文化特性,以及各種 DateTimeStyles 值來剖析數個日期和時間字串。

string dateString, format;
DateTimeOffset result;
IFormatProvider provider = CultureInfo.InvariantCulture;

// Parse date-only value with invariant culture and assume time is UTC.
dateString = "06/15/2008";
format = "d";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                 DateTimeStyles.AssumeUniversal,
                                 out result))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   Console.WriteLine("'{0}' is not in the correct format.", dateString);

// Parse date-only value with leading white space.
// Should return False because only trailing white space is
// specified in method call.
dateString = " 06/15/2008";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                 DateTimeStyles.AllowTrailingWhite,
                                 out result))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   Console.WriteLine("'{0}' is not in the correct format.", dateString);

// Parse date and time value, and allow all white space.
dateString = " 06/15/   2008  15:15    -05:00";
format = "MM/dd/yyyy H:mm zzz";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                 DateTimeStyles.AllowWhiteSpaces,
                                 out result))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   Console.WriteLine("'{0}' is not in the correct format.", dateString);

// Parse date and time and convert to UTC.
dateString = "  06/15/2008 15:15:30 -05:00";
format = "MM/dd/yyyy H:mm:ss zzz";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                DateTimeStyles.AllowWhiteSpaces |
                                DateTimeStyles.AdjustToUniversal,
                                out result))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   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 +00:00.
//    ' 06/15/2008' is not in the correct format.
//    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
//    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.
let provider = CultureInfo.InvariantCulture

// Parse date-only value with invariant culture and assume time is UTC.
let dateString = "06/15/2008"
let format = "d"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AssumeUniversal) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// Parse date-only value with leading white space.
// Should return False because only trailing white space is
// specified in method call.
let dateString = " 06/15/2008"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AllowTrailingWhite) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// Parse date and time value, and allow all white space.
let dateString = " 06/15/   2008  15:15    -05:00"
let format = "MM/dd/yyyy H:mm zzz"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AllowWhiteSpaces) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// Parse date and time and convert to UTC.
let dateString = "  06/15/2008 15:15:30 -05:00"
let format = "MM/dd/yyyy H:mm:ss zzz"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AllowWhiteSpaces ||| DateTimeStyles.AdjustToUniversal) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// The example displays the following output:
//    '06/15/2008' converts to 6/15/2008 12:00:00 AM +00:00.
//    ' 06/15/2008' is not in the correct format.
//    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
//    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.
Dim dateString, format As String  
Dim result As DateTimeOffset
Dim provider As CultureInfo = CultureInfo.InvariantCulture

' Parse date-only value with invariant culture and assume time is UTC.
dateString = "06/15/2008"
format = "d"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                   DateTimeStyles.AssumeUniversal, _
                                   result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date-only value with leading white space.
' Should return False because only trailing white space is  
' specified in method call.
dateString = " 06/15/2008"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowTrailingWhite, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date and time value, and allow all white space.
dateString = " 06/15/   2008  15:15    -05:00"
format = "MM/dd/yyyy H:mm zzz"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowWhiteSpaces, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date and time and convert to UTC.
dateString = "  06/15/2008 15:15:30 -05:00"   
format = "MM/dd/yyyy H:mm:ss zzz"       
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowWhiteSpaces Or _
                                DateTimeStyles.AdjustToUniversal, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 
' The example displays the following output:
'    '06/15/2008' converts to 6/15/2008 12:00:00 AM +00:00.
'    ' 06/15/2008' is not in the correct format.
'    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
'    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.

下列範例會使用各種 DateTimeStyles 值來剖析預期符合 ISO 8601的字串陣列。 如範例的輸出所示,如果下列情況,格式正確的字串將無法剖析:

如果未指定 UTC 位移的字串會假設有當地時區的位移 (在此案例中為 -07:00,這會反映太平洋日光節約時區的位移) ,除非 DateTimeStyles.AssumeUniversal 在方法呼叫中提供旗標。 在此情況下,它們會假設為通用協調時間。

open System
open System.Globalization

let parseWithISO8601 dateStrings styles =
    printfn $"Parsing with {styles}:"
    for dateString in dateStrings do
        match DateTimeOffset.TryParseExact(dateString, "O", null, styles) with
        | true, date ->
            printfn $"""   {dateString,-35} --> {date.ToString "yyyy-MM-dd HH:mm:ss.FF zzz"}"""
        | _ ->
            printfn $"   Unable to convert '{dateString}'"

let dateStrings = 
    [ "2018-08-18T12:45:16.0000000Z"
      "2018/08/18T12:45:16.0000000Z"
      "2018-18-08T12:45:16.0000000Z"
      "2018-08-18T12:45:16.0000000"
      " 2018-08-18T12:45:16.0000000Z "
      "2018-08-18T12:45:16.0000000+02:00"
      "2018-08-18T12:45:16.0000000-07:00" ]

parseWithISO8601 dateStrings DateTimeStyles.None
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AllowWhiteSpaces
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AdjustToUniversal
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AssumeLocal
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AssumeUniversal

// The example displays the following output:
//      Parsing with None:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AllowWhiteSpaces:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//          2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AdjustToUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
//
//      -----
//
//      Parsing with AssumeLocal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AssumeUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] dateStrings = { "2018-08-18T12:45:16.0000000Z",
                               "2018/08/18T12:45:16.0000000Z",
                               "2018-18-08T12:45:16.0000000Z",
                               "2018-08-18T12:45:16.0000000",                               
                               " 2018-08-18T12:45:16.0000000Z ",
                               "2018-08-18T12:45:16.0000000+02:00",
                               "2018-08-18T12:45:16.0000000-07:00" }; 
      
      ParseWithISO8601(dateStrings, DateTimeStyles.None);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AllowWhiteSpaces);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AdjustToUniversal);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeLocal);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeUniversal);   }

   private static void ParseWithISO8601(string[] dateStrings, DateTimeStyles styles)
   {   
      Console.WriteLine($"Parsing with {styles}:");
      DateTimeOffset date;
      foreach (var dateString in dateStrings)
      {
         if (DateTimeOffset.TryParseExact(dateString, "O", null, styles, out date))
         {
            Console.WriteLine($"   {dateString,-35} --> {date:yyyy-MM-dd HH:mm:ss.FF zzz}");
         }
         else
         {
            Console.WriteLine($"   Unable to convert '{dateString}'");
         }   
      } 
   }
}
// The example displays the following output:
//      Parsing with None:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AllowWhiteSpaces:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//          2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AdjustToUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
//
//      -----
//
//      Parsing with AssumeLocal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AssumeUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
Imports System.Globalization

Public Module Example
   Public Sub Main()
      Dim dateStrings() = { "2018-08-18T12:45:16.0000000Z",
                            "2018/08/18T12:45:16.0000000Z",
                            "2018-18-08T12:45:16.0000000Z",
                            "2018-08-18T12:45:16.0000000",                               
                            " 2018-08-18T12:45:16.0000000Z ",
                            "2018-08-18T12:45:16.0000000+02:00",
                            "2018-08-18T12:45:16.0000000-07:00" } 
      
      ParseWithISO8601(dateStrings, DateTimeStyles.None)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AllowWhiteSpaces)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AdjustToUniversal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeLocal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeUniversal)   
   End Sub

   Private Sub ParseWithISO8601(dateStrings() As String, styles As DateTimeStyles)
      Console.WriteLine($"Parsing with {styles}:")
      Dim dat As DateTimeOffset
      For Each dateStr In dateStrings
         If DateTimeOffset.TryParseExact(dateStr, "O", Nothing, styles, dat) Then
            Console.WriteLine($"   {dateStr,-35} --> {dat:yyyy-MM-dd HH:mm:ss.FF zzz}")
         Else
            Console.WriteLine($"   Unable to convert '{dateStr}'")
         End If   
      Next 
   End Sub
End Module
' The example displays the following output:
'      Parsing with None:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
'
'      -----
'
'      Parsing with AllowWhiteSpaces:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
'          2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
'
'      -----
'
'      Parsing with AdjustToUniversal:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
'
'      -----
'
'      Parsing with AssumeLocal:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
'
'      -----
'
'      Parsing with AssumeUniversal:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00

備註

方法的 TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset) 這個多載就像 DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) 方法一樣,不同之處在于此方法在轉換失敗時不會擲回例外狀況。 它會剖析日期和時間的字串表示,該字串必須完全符合 參數所 format 指定的模式。 input如果字串不符合此模式,且參數所 styles 定義的空白字元中有一些可能的變化,則剖析作業會失敗,而且方法會傳 false 回 。

參數 format 是包含單一標準格式規範或一或多個自訂格式規範的字串,可定義 的必要模式 input 。 如需有效格式化程式碼的詳細資訊,請參閱 標準日期和時間格式字串自訂日期和時間格式字串。 如果 format 包含 zzzzzz 自訂格式規範,表示位移必須存在於 input 中,該位移必須包含負號或正負號。 如果遺漏符號,剖析作業會失敗,而且方法會傳 false 回 。

如果需要 format 包含 input 日期而非時間,則產生的 DateTimeOffset 物件會指派午夜 (0:00:00) 的時間。 如果需要 format 包含 input 時間,但不包含日期,產生的 DateTimeOffset 物件會指派本機系統上的目前日期。 如果 format 不需要包含 input 位移,則產生的 DateTimeOffset 物件位移取決於 參數的值 styles 。 如果 styles 包含 AssumeLocal ,則會將本機時區的位移指派給 DateTimeOffset 物件。 如果 styles 包含 AssumeUniversal ,則會將國際標準時間 (UTC) 位移或 +00:00 指派給 DateTimeOffset 物件。 如果未指定任何值,則會使用當地時區的位移。

input 所使用的特定日期和時間符號和字串是由 參數所 formatProvider 定義。 如果 是標準格式規範字符串,則的精確模式 inputformat 也是如此。 參數 formatProvider 可以是下列其中一項:

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

參數 styles 會定義輸入字串中是否允許空白字元、指出如何剖析不含明確位移元件的字串,並支援在剖析作業期間進行 UTC 轉換。 除了 之外 NoCurrentDateDefault ,支援列舉的所有成員 DateTimeStyles 。 下表列出每個支援成員的效果。

DateTimeStyles 成員 行為
AdjustToUniversal input視需要剖析 ,並將它轉換成 UTC。 它相當於剖析字串,然後呼叫 DateTimeOffset.ToUniversalTimeDateTimeOffset 回物件的 方法。
AssumeLocal 如果 format 不需要包含 input 位移值,則傳 DateTimeOffset 回的物件會獲得當地時區的位移。 此為預設行為。
AssumeUniversal 如果 format 不需要包含 input 位移值,則會將傳 DateTimeOffset 回的物件指定為 UTC 位移 (+00:00) 。
AllowInnerWhite 允許 input 包含格式未指定的內部空白字元。 在日期和時間元件與個別元件之間,除了位移之外,也會在剖析字串時忽略額外的空白字元。
AllowLeadingWhite 允許 input 包含 未指定的 format 前置空格。 剖析字串時會忽略這些。
AllowTrailingWhite 允許 input 包含 未指定的 format 尾端空格。 剖析字串時會忽略這些。
AllowWhiteSpaces 允許 input 包含開頭、尾端和未指定的 format 內部空格。 剖析字串時,不會忽略 中 format 未指定的所有額外空白字元。
None 表示 中 input 不允許額外的空白字元。 空白字元必須與 中指定的 format 完全相同。 此為預設行為。
RoundtripKind 沒有作用,因為 DateTimeOffset 結構不包含 Kind 屬性。

給呼叫者的注意事項

在 .NET Framework 4 中,如果要剖析的字串包含小時元件,以及未同意的 AM/PM 指示項,則會 TryParseExact 傳回 false 。 在 .NET Framework 3.5 和舊版中,會忽略 AM/PM 指示項。

另請參閱

適用於