DateTimeOffset.TryParse 方法

定义

将日期和时间的指定字符串表示形式转换为其等效的 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)

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

尝试将日期和时间的指定字符串表示形式转换为其等效 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

例外

styles 包括未定义的 DateTimeStyles 值。

- 或 -

不支持 NoCurrentDateDefault

- 或 -

styles 包括互斥的 DateTimeStyles 值。

示例

以下示例使用各种DateTimeStyles值调用 TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) 方法,以分析具有各种日期和时间格式的某些字符串。

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) ,不同之处在于,如果转换失败,它不会引发异常。 方法分析包含三个元素的字符串,这些元素可以按任意顺序显示,并由空格分隔。 下表显示了这三个元素。

元素 示例
<Date> "2/10/2007"
<时间> “1:02:03 PM”
<Offset> "-7:30"

尽管其中每个元素都是可选的,但 <Offset> 不能单独显示。 它必须与 Date> 或 <Time> 一起<提供。 如果 <缺少 Date> ,则其默认值为当前日期。 如果 <Date> 存在,但其年份组件仅包含两位数字,则根据 属性的值Calendar.TwoDigitYearMax将其转换为参数当前日历中的provider年份。 如果 <缺少 Time> ,其默认值为上午 12:00:00。 如果<缺少 Offset>,则其默认值为本地时区的偏移量,或者Zero如果在 DateTimeStyles.AdjustToUniversalstyles指定了 或 DateTimeStyles.AssumeUniversal 值,则为 。 如果 <存在 Offset> ,它可以表示协调世界时 (UTC) 的负偏移量或正偏移量。 在任一情况下, <Offset> 都必须包含符号符号,否则方法返回 false

通过使用 input 参数提供的 对象中 DateTimeFormatInfo 特定于区域性的格式设置信息来分析字符串 formatProvider 。 参数 formatProvider 可以是以下任一参数:

此外,每个元素都可以由前导空格或尾随空格分隔,日期和时间<><>组件可以包含内部空格 (,例如 6:00:00) 。 <只有 Offset> 组件不能包含内部空格。

如果 providernull,则 CultureInfo 使用与当前区域性对应的 对象。

Offset> 中使用的<正号或负号必须为 + 或 -。 它不是由PositiveSign参数NumberFormat的 属性返回formatproviderNumberFormatInfo 对象的 或 NegativeSign 属性定义的。

支持枚举的 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)

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

尝试将日期和时间的指定范围表示形式转换为其等效 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)

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

尝试将字符串分析为值。

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)

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

尝试将字符范围解析为值。

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)

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

尝试将日期和时间的指定范围表示形式转换为其等效 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)

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

尝试将日期和时间的指定字符串表示形式转换为它的等效 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) 方法,只是在转换失败时不会引发异常。 它分析包含三个元素的字符串,这些元素可以按任意顺序显示,并由空格分隔。 下表显示了这三个元素。

元素 示例
<Date> "2/10/2007"
<时间> “1:02:03 PM”
<Offset> "-7:30"

尽管其中每个元素都是可选的,但 <Offset> 无法单独显示。 它必须与日期>或<时间>一起<提供。 如果 <缺少 Date> ,则其默认值为当前日期。 如果 <Date> 存在,但其年份组件仅包含两位数字,则会根据 属性的值将其转换为当前区域性的当前日历中的 Calendar.TwoDigitYearMax 年份。 如果 <缺少 Time> ,则其默认值为凌晨 12:00:00。 如果 <缺少 Offset> ,则其默认值为本地时区的偏移量。 如果 <Offset> 存在,则它可以表示与协调世界时 (UTC) 的负偏移量或正偏移量。 在任一情况下, <Offset> 都必须包含符号符号,否则方法将 false返回 。

通过使用 input 为当前区域性初始化的 对象中的 DateTimeFormatInfo 格式设置信息来分析字符串。 若要分析包含不一定对应于当前区域性的指定格式的字符串,请使用 TryParseExact 方法并提供格式说明符。

另请参阅

适用于