public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, System.Globalization.DateTimeStyles styles, out DateTime result);
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider provider, System.Globalization.DateTimeStyles styles, out DateTime result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles * DateTime -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTime) As Boolean
當這個方法傳回時,如果轉換成功,則包含相當於 中所
s
包含日期和時間的值;如果轉換失敗,則包含
DateTime
DateTime.MinValue
。 如果
s
參數為
null
、為空字串 ("") 或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] DateTime % result) = IParsable<DateTime>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out DateTime result);
static member TryParse : string * IFormatProvider * DateTime -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As DateTime) As Boolean
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] DateTime % result) = ISpanParsable<DateTime>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out DateTime result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * DateTime -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As DateTime) As Boolean
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParse (ReadOnlySpan<char> s, out DateTime result);
static member TryParse : ReadOnlySpan<char> * DateTime -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As DateTime) As Boolean
當這個方法傳回時,如果轉換成功,則包含相當於 中所
s
包含日期和時間的值;如果轉換失敗,則包含
DateTime
DateTime.MinValue
。 如果
s
參數為
null
、為空字串 ("") 或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParse (string s, out DateTime result);
public static bool TryParse (string? s, out DateTime result);
static member TryParse : string * DateTime -> bool
Public Shared Function TryParse (s As String, ByRef result As DateTime) As Boolean
當這個方法傳回時,如果轉換成功,則包含相當於 中所
s
包含日期和時間的值;如果轉換失敗,則包含
DateTime
DateTime.MinValue
。 如果
s
參數為
null
、為空字串 ("") 或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。
void main()
array<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 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);
for each (String^ dateString in dateStrings)
if (DateTime::TryParse(dateString, dateValue))
Console::WriteLine(" Converted '{0}' to {1} ({2}).", dateString,
dateValue, dateValue.Kind);
Console::WriteLine(" Unable to parse '{0}'.", dateString);
// The example displays the following output:
// 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 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).
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);
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).
open System
open System.Globalization
let 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" ]
printfn $"Attempting to parse strings using {CultureInfo.CurrentCulture.Name} culture."
for dateString in dateStrings do
match DateTime.TryParse dateString with
| true, dateValue ->
printfn $" Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $" Unable to parse '{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).
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim dateStrings() As String = {"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"}
Dim dateValue As Date
Console.WriteLine("Attempting to parse strings using {0} culture.", _
CultureInfo.CurrentCulture.Name)
For Each dateString As String In dateStrings
If Date.TryParse(dateString, dateValue) Then
Console.WriteLine(" Converted '{0}' to {1} ({2}).", dateString, _
dateValue, dateValue.Kind)
Console.WriteLine(" Unable to parse '{0}'.", dateString)
End If
End Sub
End Module
' 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
其 屬性為
DateTimeKind.Local
方法傳回時的值
Kind
。
標準日期和時間格式字串
自訂日期和時間格式字串
Sample: .NET Core WinForms Formatting Utility (C#) (範例:.NET Core WinForms 格式化公用程式 (C#))
Sample: .NET Core WinForms Formatting Utility (Visual Basic) (範例:.NET Core WinForms 格式化公用程式 (Visual Basic))
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParse (string s, IFormatProvider provider, System.Globalization.DateTimeStyles styles, out DateTime result);
public static bool TryParse (string? s, IFormatProvider? provider, System.Globalization.DateTimeStyles styles, out DateTime result);
static member TryParse : string * IFormatProvider * System.Globalization.DateTimeStyles * DateTime -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTime) As Boolean
當這個方法傳回時,如果轉換成功,則包含相當於 中所
s
包含日期和時間的值;如果轉換失敗,則包含
DateTime
DateTime.MinValue
。 如果
s
參數為
null
、為空字串 ("") 或者不包含日期和時間的有效字串表示,則轉換會失敗。 這個參數會以未初始化的狀態傳遞。
下列範例說明
DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
方法。
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);
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);
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);
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);
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);
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.
open System
open System.Globalization
[<EntryPoint>]
let main _ =
// Parse a date and time with no styles.
let dateString = "03/01/2009 10:00 AM"
let culture = CultureInfo.CreateSpecificCulture "en-US"
let styles = DateTimeStyles.None
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
// Parse the same date and time with the AssumeLocal style.
let styles = DateTimeStyles.AssumeLocal
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
// 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.
let dateString = "2009/03/01T10:00:00-5:00"
let styles = DateTimeStyles.AssumeLocal
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
// Attempt to convert a string in improper ISO 8601 format.
let dateString = "03/01/2009T10:00:00-5:00"
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
// Assume a date and time string formatted for the fr-FR culture is the local
// time and convert it to UTC.
let dateString = "2008-03-01 10:00"
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
let styles = DateTimeStyles.AdjustToUniversal ||| DateTimeStyles.AssumeLocal
match DateTime.TryParse(dateString, culture, styles) with
| true, dateResult ->
printfn $"{dateString} converted to {dateResult} {dateResult.Kind}."
| _ ->
printfn $"Unable to convert {dateString} to a date and time."
// 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.
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim dateString As String
Dim culture As CultureInfo
Dim styles As DateTimeStyles
Dim dateResult As DateTime
' 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, dateResult) Then
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
' Parse the same date and time with the AssumeLocal style.
styles = DateTimeStyles.AssumeLocal
If DateTime.TryParse(dateString, culture, styles, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
' 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, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
' Attempt to convert a string in improper ISO 8601 format.
dateString = "03/01/2009T10:00:00-5:00"
If DateTime.TryParse(dateString, culture, styles, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
' 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 Or DateTimeStyles.AssumeLocal
If DateTime.TryParse(dateString, culture, styles, dateResult)
Console.WriteLine("{0} converted to {1} {2}.", _
dateString, dateResult, dateResult.Kind)
Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
End If
End Sub
End Module
' 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.
方法
DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
會剖析可包含日期、時間和時區資訊的字串。 這與
DateTime.Parse(String, IFormatProvider, DateTimeStyles)
方法類似,不同之處在于,如果轉換失敗,方法
DateTime.TryParse(String, DateTime)
不會擲回例外狀況。
這個方法會嘗試忽略無法辨識的資料,並完全剖析
s
。 如果
s
包含時間但沒有日期,則方法預設會取代目前的日期,如果
styles
包含
NoCurrentDateDefault
旗標,則會取代
DateTime.Date.MinValue
。 如果
s
包含日期但沒有時間,則會使用午夜 12:00 做為預設時間。 如果日期存在,但其年份元件只包含兩位數,則會根據 屬性的值
Calendar.TwoDigitYearMax
,轉換成參數目前行事曆中的
provider
年份。 中
s
任何前置、內部或尾端空白字元都會被忽略。 日期和時間可以用一對開頭和尾端的 NUMBER SIGN 字元括住, ('#'、U+0023) ,並以一或多個 Null 字元結尾, (U+0000) 。
參數會定義
provider
日期和時間元素的特定有效格式,以及日期和時間中使用的名稱和符號,可以是下列任一項:
CultureInfo
物件,表示其格式設定用於 參數的文化
s
特性。 屬性
DateTimeFormatInfo
所
CultureInfo.DateTimeFormat
傳回的物件會定義 中使用的
s
格式設定。
DateTimeFormatInfo
物件,定義 中使用的
s
格式設定。
自訂
IFormatProvider
實作。 其
IFormatProvider.GetFormat
方法會傳
DateTimeFormatInfo
回 物件,定義 中使用的
s
格式設定。
如果
provider
是
null
,則會使用目前的文化特性。
如果
s
是目前日曆中閏年的閏日字串標記法,則方法會成功剖析
s
。 如果
s
是 目前行事曆
provider
中非閏年之閏日字串標記法,則剖析作業會失敗,而且 方法會傳
false
回 。
參數
styles
會定義剖析字串的精確解譯,以及剖析作業應該如何處理它。 它可以是列舉的
DateTimeStyles
一或多個成員,如下表所述。
DateTimeStyles 成員
AdjustToUniversal
視需要剖析
s
,並將它轉換成 UTC。 如果
s
包含時區位移,或如果
s
不包含時區資訊,但
styles
包含
DateTimeStyles.AssumeLocal
旗標,方法會剖析字串、呼叫
ToUniversalTime
以將傳
DateTime
回的值轉換為 UTC,並將 屬性設定
Kind
為
DateTimeKind.Utc
。 如果
s
表示它代表 UTC,或者如果
s
不包含時區資訊,但
styles
包含
DateTimeStyles.AssumeUniversal
旗標,則 方法會剖析字串、在傳
DateTime
回的值上不執行時區轉換,並將 屬性設定
Kind
為
DateTimeKind.Utc
。 在其他所有情況下,旗標沒有任何作用。
AllowInnerWhite
雖然有效,但會忽略此值。 在 的
s
日期和時間元素中,允許內部空白字元。
AllowLeadingWhite
雖然有效,但會忽略此值。 在 的
s
日期和時間元素中允許前置空白字元。
AllowTrailingWhite
雖然有效,但會忽略此值。 在 的
s
日期和時間元素中,允許尾端空白字元。
AllowWhiteSpaces
指定可能包含
s
前置、內部和尾端空白字元。 這是預設行為。 無法藉由提供更嚴格的
DateTimeStyles
列舉值來覆寫它,例如
DateTimeStyles.None
。
AssumeLocal
指定如果
s
缺少任何時區資訊,則會假設它代表當地時間。 除非有
DateTimeStyles.AdjustToUniversal
旗標,
Kind
否則傳
DateTime
回值的 屬性會設定為
DateTimeKind.Local
。
AssumeUniversal
指定如果
s
缺少任何時區資訊,則會假設它代表 UTC。
DateTimeStyles.AdjustToUniversal
除非有 旗標,否則 方法會將傳
DateTime
回的值從 UTC 轉換為當地時間,並將其 屬性設定
Kind
為
DateTimeKind.Local
。
雖然有效,但會忽略此值。
RoundtripKind
對於包含時區資訊的字串,嘗試防止將日期和時間字串
DateTime
轉換成其
Kind
屬性設定為
DateTimeKind.Local
的值。 一般而言,這類字串是藉由使用 「o」、「r」 或 「u」 標準格式規範呼叫
DateTime.ToString(String)
方法來建立。
如果
s
不包含時區資訊,則方法會
DateTime
傳回其 屬性為
DateTimeKind.Unspecified
的值
Kind
,
DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
除非
styles
另有指示旗標。 如果
s
包含時區或時區位移資訊,此方法
DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
會執行任何必要的時間轉換,並傳回下列其中一項:
值
DateTime
,其日期和時間反映當地時間,且其
Kind
屬性為
DateTimeKind.Local
。
或者,如果
styles
包含
AdjustToUniversal
旗標,則為
DateTime
值,其日期和時間會反映 UTC,且其
Kind
屬性為
DateTimeKind.Utc
。
您可以使用 旗標覆
DateTimeStyles.RoundtripKind
寫此行為。
剖析自訂文化特性
如果您剖析針對自訂文化特性產生的日期和時間字串,請使用
TryParseExact
方法,而不是
TryParse
方法來改善剖析作業成功的可能性。 自訂文化特性日期和時間字串可能很複雜且難以剖析。 方法
TryParse
會嘗試剖析具有數個隱含剖析模式的字串,其中所有模式都可能會失敗。 相反地,方法
TryParseExact
會要求您明確指定一或多個可能成功的確切剖析模式。
如需自訂文化特性的詳細資訊,請參閱 類別
System.Globalization.CultureAndRegionInfoBuilder
。