DateTimeOffset.TryParse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將日期和時間之指定的字串表示轉換為其對等的 DateTimeOffset。
多載
TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) |
嘗試將日期和時間的指定字串表示轉換為其相等的 DateTimeOffset,並傳回表示轉換是否成功的值。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset) |
嘗試將日期和時間的指定範圍表示轉換為其對等的 DateTimeOffset,並傳回值以指出轉換是否成功。 |
TryParse(String, IFormatProvider, DateTimeOffset) |
嘗試將字串剖析成值。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeOffset) |
嘗試將字元範圍剖析為值。 |
TryParse(ReadOnlySpan<Char>, DateTimeOffset) |
嘗試將日期和時間的指定範圍表示轉換為其對等的 DateTimeOffset,並傳回值以指出轉換是否成功。 |
TryParse(String, DateTimeOffset) |
嘗試將日期和時間之指定的字串表示轉換為其相等的 DateTimeOffset,並傳回一個值,指出轉換是否成功。 |
TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset)
嘗試將日期和時間的指定字串表示轉換為其相等的 DateTimeOffset,並傳回表示轉換是否成功的值。
public:
static bool TryParse(System::String ^ input, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParse (string input, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParse (string? input, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParse : string * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParse (input As String, formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean
參數
- input
- String
字串,包含要轉換的日期和時間。
- formatProvider
- IFormatProvider
提供關於 input
之特定文化特性格式資訊的物件。
- styles
- DateTimeStyles
列舉值的位元組合,其表示 input
所允許的格式。
- result
- DateTimeOffset
當方法傳回時,如果轉換成功,則包含 DateTimeOffset 相當於 日期和時間的值 input
,如果轉換失敗,則為 DateTimeOffset.MinValue。 如果 input
參數為 null
,或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。
傳回
如果 input
參數轉換成功,則為 true
;否則為 false
。
例外狀況
範例
下列範例會使用各種值呼叫 TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) 方法,以剖析具有各種 DateTimeStyles 日期和時間格式的一些字串。
string dateString;
DateTimeOffset parsedDate;
dateString = "05/01/2008 6:00:00";
// Assume time is local
if (DateTimeOffset.TryParse(dateString, null as IFormatProvider,
DateTimeStyles.AssumeLocal,
out parsedDate))
Console.WriteLine("'{0}' was converted to {1}.",
dateString, parsedDate.ToString());
else
Console.WriteLine("Unable to parse '{0}'.", dateString);
// Assume time is UTC
if (DateTimeOffset.TryParse(dateString, null as IFormatProvider,
DateTimeStyles.AssumeUniversal,
out parsedDate))
Console.WriteLine("'{0}' was converted to {1}.",
dateString, parsedDate.ToString());
else
Console.WriteLine("Unable to parse '{0}'.", dateString);
// Parse and convert to UTC
dateString = "05/01/2008 6:00:00AM +5:00";
if (DateTimeOffset.TryParse(dateString, null as IFormatProvider,
DateTimeStyles.AdjustToUniversal,
out parsedDate))
Console.WriteLine("'{0}' was converted to {1}.",
dateString, parsedDate.ToString());
else
Console.WriteLine("Unable to parse '{0}'.", dateString);
// The example displays the following output to the console:
// '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM -07:00.
// '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM +00:00.
// '05/01/2008 6:00:00AM +5:00' was converted to 5/1/2008 1:00:00 AM +00:00.
let dateString = "05/01/2008 6:00:00"
// Assume time is local
match DateTimeOffset.TryParse(dateString, null, DateTimeStyles.AssumeLocal) with
| true, parsedDate ->
printfn $"'{dateString}' was converted to {parsedDate}."
| _ ->
printfn $"Unable to parse '{dateString}'."
// Assume time is UTC
match DateTimeOffset.TryParse(dateString, null, DateTimeStyles.AssumeUniversal) with
| true, parsedDate ->
printfn $"'{dateString}' was converted to {parsedDate}."
| _ ->
printfn $"Unable to parse '{dateString}'."
// Parse and convert to UTC
let dateString = "05/01/2008 6:00:00AM +5:00"
match DateTimeOffset.TryParse(dateString, null, DateTimeStyles.AdjustToUniversal) with
| true, parsedDate ->
printfn $"'{dateString}' was converted to {parsedDate}."
| _ ->
printfn $"Unable to parse '{dateString}'."
// The example displays the following output to the console:
// '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM -07:00.
// '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM +00:00.
// '05/01/2008 6:00:00AM +5:00' was converted to 5/1/2008 1:00:00 AM +00:00.
Dim dateString As String
Dim parsedDate As DateTimeOffset
dateString = "05/01/2008 6:00:00"
' Assume time is local
If DateTimeOffset.TryParse(dateString, Nothing, _
DateTimeStyles.AssumeLocal, _
parsedDate) Then
Console.WriteLine("'{0}' was converted to {1}.", _
dateString, parsedDate.ToString())
Else
Console.WriteLine("Unable to parse '{0}'.", dateString)
End If
' Assume time is UTC
If DateTimeOffset.TryParse(dateString, Nothing, _
DateTimeStyles.AssumeUniversal, _
parsedDate) Then
Console.WriteLine("'{0}' was converted to {1}.", _
dateString, parsedDate.ToString())
Else
Console.WriteLine("Unable to parse '{0}'.", dateString)
End If
' Parse and convert to UTC
dateString = "05/01/2008 6:00:00AM +5:00"
If DateTimeOffset.TryParse(dateString, Nothing, _
DateTimeStyles.AdjustToUniversal, _
parsedDate) Then
Console.WriteLine("'{0}' was converted to {1}.", _
dateString, parsedDate.ToString())
Else
Console.WriteLine("Unable to parse '{0}'.", dateString)
End If
' The example displays the following output to the console:
' '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM -07:00.
' '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM +00:00.
' '05/01/2008 6:00:00AM +5:00' was converted to 5/1/2008 1:00:00 AM +00:00.
備註
此方法的 TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) 這個多載就像 DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) 方法一樣,不同之處在于,如果轉換失敗,它不會擲回例外狀況。 方法會剖析具有三個元素的字串,這些元素可以依任何順序顯示,並以空白字元分隔。 下表顯示這三個元素。
元素 | 範例 |
---|---|
<日期> | "2/10/2007" |
<Time> | 「1:02:03 PM」 |
<Offset> | "-7:30" |
雖然每個元素都是選擇性的,但 < Offset > 本身無法顯示。 它必須與日期 > 或 < 時間 > 一 < 起提供。 如果 < 遺漏 Date > ,其預設值為目前日期。 如果 < Date > 存在,但其年份元件只包含兩位數,則會根據 屬性的值 Calendar.TwoDigitYearMax ,轉換成參數目前行事曆中的 provider
年份。 如果 < 遺漏 Time > ,其預設值為上午 12:00:00。 如果 < 遺漏 Offset > ,則其預設值為當地時區的位移,或 Zero 如果 DateTimeStyles.AdjustToUniversalDateTimeStyles.AssumeUniversal 或 值是在 中 styles
指定,則為 。 >如果 < Offset 存在,它可以代表來自國際標準時間的負數或正位移, (UTC) 。 不論是哪一種情況, < Offset > 都必須包含符號符號,否則方法會傳 false
回 。
字串是 input
使用 參數所提供 formatProvider
之 物件中的 DateTimeFormatInfo 特定文化特性格式資訊來剖析。 參數 formatProvider
可以是下列其中一項:
CultureInfo物件,表示格式設定在 中使用的
input
文化特性。 DateTimeFormatInfo屬性所 CultureInfo.DateTimeFormat 傳回的物件會定義 中input
所使用的格式。DateTimeFormatInfo物件,定義日期和時間資料的格式。
此外,每個元素都可以以前置或尾端空白字元分隔,而 < 日期和時間 ><> 元件可以包含內部空白字元 (,例如 6:00:00) 。 <只有 Offset > 元件不能包含內部空白字元。
如果 為 provider
null
,則會 CultureInfo 使用對應至目前文化特性的物件。
Offset > 中使用的 < 正負號必須是 + 或 -。 它不是由 PositiveSign 參數 NumberFormat 之 屬性所 formatprovider
傳回之 物件的 或 NegativeSign 屬性 NumberFormatInfo 所定義。
支援下列列舉成員 DateTimeStyles :
DateTimeStyles 成員 | 註解 |
---|---|
AdjustToUniversal | 剖析 所 input 代表的字串,並視需要將其轉換為 UTC。 它相當於剖析字串,然後呼叫傳回物件的 ToUniversalTime() 方法。 |
AllowInnerWhite | 雖然有效,但會忽略此值。 日期和時間 ><> 元件中 < 允許內部空白字元。 |
AllowLeadingWhite | 雖然有效,但會忽略此值。 在剖析字串中的每個元件前面允許前置空白字元。 |
AllowTrailingWhite | 雖然有效,但會忽略此值。 在剖析字串中的每個元件前面允許尾端空白字元。 |
AllowWhiteSpaces | 此為預設行為。 無法藉由提供更嚴格的 DateTimeStyles 列舉值來覆寫它,例如 DateTimeStyles.None 。 |
AssumeLocal | 表示,如果 input 參數缺少 < Offset > 元素,則應該提供當地時區的位移。 這是 方法的預設行為 TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) 。 |
AssumeUniversal | 表示,如果 input 參數缺少 < Offset > 元素,應該提供 UTC 位移 (00:00) 。 |
None | 雖然有效,但會忽略這個值,而且沒有任何作用。 |
RoundtripKind | DateTimeOffset因為 結構不包含 Kind 屬性,所以這個值沒有任何作用。 |
DateTimeStyles.NoCurrentDateDefault僅支援值。 ArgumentException如果這個值包含在 參數中 styles
,則會擲回 。
另請參閱
適用於
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset)
嘗試將日期和時間的指定範圍表示轉換為其對等的 DateTimeOffset,並傳回值以指出轉換是否成功。
public:
static bool TryParse(ReadOnlySpan<char> input, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParse (ReadOnlySpan<char> input, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParse (ReadOnlySpan<char> input, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean
參數
- input
- ReadOnlySpan<Char>
包含字元的範圍,其表示要轉換的日期和時間。
- formatProvider
- IFormatProvider
提供關於 input
之特定文化特性格式資訊的物件。
- styles
- DateTimeStyles
列舉值的位元組合,其表示 input
所允許的格式。
- result
- DateTimeOffset
當方法傳回時,如果轉換成功,則包含 DateTimeOffset 相當於 日期和時間的值 input
,如果轉換失敗,則為 DateTimeOffset.MinValue。 如果 input
參數為 null
,或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。
傳回
如果 input
參數轉換成功,則為 true
;否則為 false
。
適用於
TryParse(String, IFormatProvider, DateTimeOffset)
嘗試將字串剖析成值。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] DateTimeOffset % result) = IParsable<DateTimeOffset>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out DateTimeOffset result);
static member TryParse : string * IFormatProvider * DateTimeOffset -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As DateTimeOffset) As Boolean
參數
- s
- String
要剖析的字串。
- provider
- IFormatProvider
提供關於 s
之特定文化特性格式資訊的物件。
- result
- DateTimeOffset
當這個方法傳回時,包含成功剖析 s
或失敗時未定義值的結果。
傳回
true
如果 s
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeOffset)
嘗試將字元範圍剖析為值。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] DateTimeOffset % result) = ISpanParsable<DateTimeOffset>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out DateTimeOffset result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * DateTimeOffset -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As DateTimeOffset) As Boolean
參數
- s
- ReadOnlySpan<Char>
要剖析的字元範圍。
- provider
- IFormatProvider
提供關於 s
之特定文化特性格式資訊的物件。
- result
- DateTimeOffset
當這個方法傳回時,包含成功剖析 s
的結果,或失敗時未定義的值。
傳回
true
如果 s
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(ReadOnlySpan<Char>, DateTimeOffset)
嘗試將日期和時間的指定範圍表示轉換為其對等的 DateTimeOffset,並傳回值以指出轉換是否成功。
public:
static bool TryParse(ReadOnlySpan<char> input, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParse (ReadOnlySpan<char> input, out DateTimeOffset result);
static member TryParse : ReadOnlySpan<char> * DateTimeOffset -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), ByRef result As DateTimeOffset) As Boolean
參數
- input
- ReadOnlySpan<Char>
包含字元的範圍,其表示要轉換的日期和時間。
- result
- DateTimeOffset
當方法傳回時,如果轉換成功,則包含 DateTimeOffset 相當於 的日期和時間 input
,如果轉換失敗,則為 DateTimeOffset.MinValue。 如果 input
參數為 null
,或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。
傳回
如果 input
參數轉換成功,則為 true
;否則為 false
。
適用於
TryParse(String, DateTimeOffset)
嘗試將日期和時間之指定的字串表示轉換為其相等的 DateTimeOffset,並傳回一個值,指出轉換是否成功。
public:
static bool TryParse(System::String ^ input, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParse (string input, out DateTimeOffset result);
public static bool TryParse (string? input, out DateTimeOffset result);
static member TryParse : string * DateTimeOffset -> bool
Public Shared Function TryParse (input As String, ByRef result As DateTimeOffset) As Boolean
參數
- input
- String
字串,包含要轉換的日期和時間。
- result
- DateTimeOffset
當方法傳回時,如果轉換成功,則包含 DateTimeOffset 相當於 的日期和時間 input
,如果轉換失敗,則為 DateTimeOffset.MinValue。 如果 input
參數為 null
,或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。
傳回
如果 input
參數轉換成功,則為 true
;否則為 false
。
範例
下列範例會 TryParse(String, DateTimeOffset) 呼叫 方法來剖析具有各種日期和時間格式的數個字串。
DateTimeOffset parsedDate;
string dateString;
// String with date only
dateString = "05/01/2008";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
Console.WriteLine("{0} was converted to {1}.",
dateString, parsedDate);
// String with time only
dateString = "11:36 PM";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
Console.WriteLine("{0} was converted to {1}.",
dateString, parsedDate);
// String with date and offset
dateString = "05/01/2008 +7:00";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
Console.WriteLine("{0} was converted to {1}.",
dateString, parsedDate);
// String with day abbreviation
dateString = "Thu May 01, 2008";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
Console.WriteLine("{0} was converted to {1}.",
dateString, parsedDate);
// String with date, time with AM/PM designator, and offset
dateString = "5/1/2008 10:00 AM -07:00";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
Console.WriteLine("{0} was converted to {1}.",
dateString, parsedDate);
// if (run on 3/29/07, the example displays the following output
// to the console:
// 05/01/2008 was converted to 5/1/2008 12:00:00 AM -07:00.
// 11:36 PM was converted to 3/29/2007 11:36:00 PM -07:00.
// 05/01/2008 +7:00 was converted to 5/1/2008 12:00:00 AM +07:00.
// Thu May 01, 2008 was converted to 5/1/2008 12:00:00 AM -07:00.
// 5/1/2008 10:00 AM -07:00 was converted to 5/1/2008 10:00:00 AM -07:00.
// String with date only
let dateString = "05/01/2008"
match DateTimeOffset.TryParse dateString with
| true, parsedDate ->
printfn $"{dateString} was converted to {parsedDate}."
| _ -> ()
// String with time only
let dateString = "11:36 PM"
match DateTimeOffset.TryParse dateString with
| true, parsedDate ->
printfn $"{dateString} was converted to {parsedDate}."
| _ -> ()
// String with date and offset
let dateString = "05/01/2008 +7:00"
match DateTimeOffset.TryParse dateString with
| true, parsedDate ->
printfn $"{dateString} was converted to {parsedDate}."
| _ -> ()
// String with day abbreviation
let dateString = "Thu May 01, 2008"
match DateTimeOffset.TryParse dateString with
| true, parsedDate ->
printfn $"{dateString} was converted to {parsedDate}."
| _ -> ()
// String with date, time with AM/PM designator, and offset
let dateString = "5/1/2008 10:00 AM -07:00"
match DateTimeOffset.TryParse dateString with
| true, parsedDate ->
printfn $"{dateString} was converted to {parsedDate}."
| _ -> ()
// if (run on 3/29/07, the example displays the following output
// to the console:
// 05/01/2008 was converted to 5/1/2008 12:00:00 AM -07:00.
// 11:36 PM was converted to 3/29/2007 11:36:00 PM -07:00.
// 05/01/2008 +7:00 was converted to 5/1/2008 12:00:00 AM +07:00.
// Thu May 01, 2008 was converted to 5/1/2008 12:00:00 AM -07:00.
// 5/1/2008 10:00 AM -07:00 was converted to 5/1/2008 10:00:00 AM -07:00.
Dim parsedDate As DateTimeOffset
Dim dateString As String
' String with date only
dateString = "05/01/2008"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
Console.WriteLine("{0} was converted to {1}.", _
dateString, parsedDate)
' String with time only
dateString = "11:36 PM"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
Console.WriteLine("{0} was converted to {1}.", _
dateString, parsedDate)
' String with date and offset
dateString = "05/01/2008 +7:00"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
Console.WriteLine("{0} was converted to {1}.", _
dateString, parsedDate)
' String with day abbreviation
dateString = "Thu May 01, 2008"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
Console.WriteLine("{0} was converted to {1}.", _
dateString, parsedDate)
' String with date, time with AM/PM designator, and offset
dateString = "5/1/2008 10:00 AM -07:00"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
Console.WriteLine("{0} was converted to {1}.", _
dateString, parsedDate)
' If run on 3/29/07, the example displays the following output
' to the console:
' 05/01/2008 was converted to 5/1/2008 12:00:00 AM -07:00.
' 11:36 PM was converted to 3/29/2007 11:36:00 PM -07:00.
' 05/01/2008 +7:00 was converted to 5/1/2008 12:00:00 AM +07:00.
' Thu May 01, 2008 was converted to 5/1/2008 12:00:00 AM -07:00.
' 5/1/2008 10:00 AM -07:00 was converted to 5/1/2008 10:00:00 AM -07:00.
備註
此方法的 TryParse(String, DateTimeOffset) 這個多載就像 DateTimeOffset.Parse(String) 方法一樣,不同之處在于,如果轉換失敗,它不會擲回例外狀況。 它會剖析具有三個元素的字串,這些元素可以依任何順序顯示,並以空白字元分隔。 下表顯示這三個元素。
元素 | 範例 |
---|---|
<日期> | "2/10/2007" |
<Time> | 「1:02:03 PM」 |
<Offset> | "-7:30" |
雖然每個元素都是選擇性的,但 < Offset > 本身無法顯示。 它必須與日期 > 或 < 時間 > 一 < 起提供。 如果 < 遺漏 Date > ,其預設值為目前日期。 如果 < Date > 存在,但其年份元件只包含兩位數,則會根據 屬性的值 Calendar.TwoDigitYearMax ,轉換成目前文化特性目前行事曆中的年份。 如果 < 遺漏 Time > ,其預設值為上午 12:00:00。 如果 < 遺漏 Offset > ,其預設值為當地時區的位移。 >如果 < Offset 存在,它可以代表來自國際標準時間的負數或正位移, (UTC) 。 不論是哪一種情況, < Offset > 都必須包含符號符號,否則方法會傳 false
回 。
字串會 input
使用針對目前文化特性初始化的 物件中的 DateTimeFormatInfo 格式資訊進行剖析。 若要剖析包含不一定對應至目前文化特性之指定格式的字串,請使用 TryParseExact 方法並提供格式規範。
另請參閱
- Parse
- Sample: .NET Core WinForms Formatting Utility (C#) (範例:.NET Core WinForms 格式化公用程式 (C#))
- Sample: .NET Core WinForms Formatting Utility (Visual Basic) (範例:.NET Core WinForms 格式化公用程式 (Visual Basic))
適用於
意見反應
提交並檢視相關的意見反應