DateTime.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将日期和时间的指定字符串表示形式转换为等效 DateTime 项,并返回一个值,该值指示转换是否成功。
重载
| 名称 | 说明 |
|---|---|
| TryParse(ReadOnlySpan<Char>, DateTime) |
将日期和时间的指定字符范围转换为其 DateTime 等效项,并返回一个值,该值指示转换是否成功。 |
| TryParse(String, DateTime) |
将日期和时间的指定字符串表示形式转换为等效 DateTime 项,并返回一个值,该值指示转换是否成功。 |
| TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime) |
尝试将字符范围分析为值。 |
| TryParse(String, IFormatProvider, DateTime) |
尝试将字符串分析为值。 |
| TryParse(String, IFormatProvider, DateTimeStyles, DateTime) |
使用指定的区域性特定的格式信息和格式样式将日期和时间的指定字符串表示形式转换为等效 DateTime 格式,并返回一个值,该值指示转换是否成功。 |
| TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime) |
使用指定的区域性特定的格式信息和格式样式将日期和时间的跨度表示形式转换为等效 DateTime 格式,并返回一个值,该值指示转换是否成功。 |
注解
Important
日本日历中的纪元基于皇帝的统治,因此预计将改变。 例如,2019 年 5 月 1 日在 JapaneseCalendar 和 JapaneseLunisolarCalendar 中标志着令和年号的开始。 这种时代变化会影响使用这些日历的所有应用程序。 有关详细信息以及确定您的应用程序是否受到影响,请参阅 在 .NET 中处理日语日历的新纪元。 有关在 Windows 系统上测试应用程序以确保其为纪元更改做好准备的信息,请参阅 为日本时代更改准备应用程序。 有关支持具有多个纪元的日历以及处理支持多个纪元的日历的最佳做法的 .NET 中的功能,请参阅 “使用纪元”。
TryParse(ReadOnlySpan<Char>, DateTime)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
将日期和时间的指定字符范围转换为其 DateTime 等效项,并返回一个值,该值指示转换是否成功。
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
- ReadOnlySpan<Char>
包含要转换的日期和时间的字符串。
- result
- DateTime
此方法返回时,如果 DateTime 转换成功,则包含等效于所 s包含日期和时间的值;如果转换失败,则包含 DateTime.MinValue 。 如果 s 参数为 null空字符串(“”),或不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未初始化传递。
返回
适用于
TryParse(String, DateTime)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
将日期和时间的指定字符串表示形式转换为等效 DateTime 项,并返回一个值,该值指示转换是否成功。
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
- String
包含要转换的日期和时间的字符串。
- result
- DateTime
此方法返回时,如果 DateTime 转换成功,则包含等效于所 s包含日期和时间的值;如果转换失败,则包含 DateTime.MinValue 。 如果 s 参数为 null空字符串(“”),或不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未初始化传递。
返回
示例
以下示例将多个日期和时间字符串传递给 DateTime.TryParse(String, DateTime) 该方法。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] dateStrings = {"05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8",
"2009-05-01T14:57:32.8375298-04:00", "5/01/2008",
"5/01/2008 14:57:32.80 -07:00",
"1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM",
"Fri, 15 May 2009 20:10:57 GMT" };
DateTime dateValue;
Console.WriteLine("Attempting to parse strings using {0} culture.",
CultureInfo.CurrentCulture.Name);
foreach (string dateString in dateStrings)
{
if (DateTime.TryParse(dateString, out dateValue))
Console.WriteLine(" Converted '{0}' to {1} ({2}).", dateString,
dateValue, dateValue.Kind);
else
Console.WriteLine(" Unable to parse '{0}'.", dateString);
}
}
}
// The example displays output like the following:
// Attempting to parse strings using en-US culture.
// Converted '05/01/2009 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01T14:57:32.8375298-04:00' to 5/1/2009 11:57:32 AM (Local).
//
// Converted '5/01/2008' to 5/1/2008 12:00:00 AM (Unspecified).
// Converted '5/01/2008 14:57:32.80 -07:00' to 5/1/2008 2:57:32 PM (Local).
// Converted '1 May 2008 2:57:32.8 PM' to 5/1/2008 2:57:32 PM (Unspecified).
// Unable to parse '16-05-2009 1:00:32 PM'.
// Converted 'Fri, 15 May 2009 20:10:57 GMT' to 5/15/2009 1:10:57 PM (Local).
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)
Else
Console.WriteLine(" Unable to parse '{0}'.", dateString)
End If
Next
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 。 日期和时间可以用一对前导和尾随的数字符号('#',U+0023)括起来,并且后面可以接上一个或多个 NULL 字符(U+0000)。
DateTime.TryParse(String, DateTime)由于该方法尝试使用当前区域性的格式规则分析日期和时间的字符串表示形式,因此尝试跨不同区域性分析特定字符串可能会失败或返回不同的结果。 如果特定日期和时间格式将跨不同的区域设置进行分析,请使用 DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) 该方法或方法的 TryParseExact 重载之一并提供格式说明符。
如果 s 是当前日历中闰年闰日的字符串表示形式,则该方法会成功分析 s 。 如果 s 当前区域性的当前日历中非 leap 年份中的 leap 日字符串表示形式,则分析操作将失败,并且该方法返回 false。
如果s不包含时区信息,result则包含其DateTimeKind属性为DateTimeKind.Unspecified该方法返回时的值。 如果要分析的字符串包含时区信息,result则包含其DateTime属性返回Kind时的值DateTimeKind.Local。
调用方说明
格式设置受当前 DateTimeFormatInfo 对象的属性的影响,该属性默认派生自控制面板中的 “区域和语言选项” 项。 如果当前TryParse属性设置为相同的值,该方法False可能会意外失败并DateSeparator返回TimeSeparator。
另请参阅
- Parse
- CultureInfo
- DateTimeFormatInfo
- 分析 .NET Framework 中的日期和时间字符串
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串
- 示例:.NET Core WinForms 格式设置实用工具 (C#)
- 示例:.NET Core WinForms 格式设置实用工具 (Visual Basic)
适用于
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
尝试将字符范围分析为值。
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
参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关区域性特定的格式设置信息 s。
- result
- DateTime
此方法返回时,包含成功分析 s的结果或失败时未定义的值。
返回
适用于
TryParse(String, IFormatProvider, DateTime)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
尝试将字符串分析为值。
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
参数
- s
- String
要分析的字符串。
- provider
- IFormatProvider
一个对象,提供有关区域性特定的格式设置信息 s。
- result
- DateTime
此方法返回时,包含成功分析 s 的结果或失败时未定义的值。
返回
适用于
TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的区域性特定的格式信息和格式样式将日期和时间的指定字符串表示形式转换为等效 DateTime 格式,并返回一个值,该值指示转换是否成功。
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
- String
包含要转换的日期和时间的字符串。
- provider
- IFormatProvider
一个对象,提供有关区域性特定的格式设置信息 s。
- styles
- DateTimeStyles
枚举值的按位组合,用于定义如何解释与当前时区或当前日期相关的已分析日期。 要指定 None的典型值为 。
- result
- DateTime
此方法返回时,如果 DateTime 转换成功,则包含等效于所 s包含日期和时间的值;如果转换失败,则包含 DateTime.MinValue 。 如果 s 参数为 null空字符串(“”),或不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未初始化传递。
返回
例外
provider 是中性区域性,不能用于分析操作。
示例
下面的示例演示了该方法 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);
else
Console.WriteLine("Unable to convert {0} to a date and time.",
dateString);
// Parse the same date and time with the AssumeLocal style.
styles = DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Parse a date and time that is assumed to be local.
// This time is five hours behind UTC. The local system's time zone is
// eight hours behind UTC.
dateString = "2009/03/01T10:00:00-5:00";
styles = DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Attempt to convert a string in improper ISO 8601 format.
dateString = "03/01/2009T10:00:00-5:00";
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Assume a date and time string formatted for the fr-FR culture is the local
// time and convert it to UTC.
dateString = "2008-03-01 10:00";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
}
}
// The example displays the following output to the console:
// 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Unspecified.
// 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Local.
// 2009/03/01T10:00:00-5:00 converted to 3/1/2009 7:00:00 AM Local.
// Unable to convert 03/01/2009T10:00:00-5:00 to a date and time.
// 2008-03-01 10:00 converted to 3/1/2008 6:00:00 PM Utc.
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."
0
// 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)
Else
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)
Else
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)
Else
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)
Else
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)
Else
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 用作默认时间。 如果某个日期存在,但其年份部分仅包含两位数字,则会根据provider属性的值将其转换为Calendar.TwoDigitYearMax参数当前日历中的一个年份。 将忽略 s 中的任何前导、内部或尾随空格字符。 日期和时间可以用一对前导和尾随的数字符号('#',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 成员 | Description |
|---|---|
| AdjustToUniversal | 分析 s 并在必要时将其转换为 UTC。 如果 s 包含时区偏移量,或者 s 如果不包含时区信息,但 styles 包含 DateTimeStyles.AssumeLocal 标志,该方法将分析字符串,调用 ToUniversalTime 将返回 DateTime 的值转换为 UTC,并将属性设置为 KindDateTimeKind.Utc。 如果 s 指示它表示 UTC,或者 s 如果不包含时区信息但 styles 包含 DateTimeStyles.AssumeUniversal 标志,该方法将分析字符串,对返回 DateTime 的值不执行时区转换,并将属性设置为 KindDateTimeKind.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。 |
| None | 虽然有效,但忽略此值。 |
| RoundtripKind | 对于包含时区信息的字符串,尝试阻止将日期和时间字符串转换为 DateTime 值,并将其 Kind 属性设置为 DateTimeKind.Local。 通常,此类字符串是使用“o”、“r”或“u”标准格式说明符调用 DateTime.ToString(String) 方法创建的。 |
如果 s 不包含时区信息,则 DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) 该方法将返回 DateTime 其 Kind 属性为 DateTimeKind.Unspecified 的值,除非 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类。
调用方说明
格式受当前 DateTimeFormatInfo 对象的属性的影响,该属性由 provider 参数提供。 如果当前TryParse属性设置为相同的值,该方法False可能会意外失败并DateSeparator返回TimeSeparator。
另请参阅
适用于
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的区域性特定的格式信息和格式样式将日期和时间的跨度表示形式转换为等效 DateTime 格式,并返回一个值,该值指示转换是否成功。
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
- ReadOnlySpan<Char>
一个范围,包含表示要转换的日期和时间的字符。
- provider
- IFormatProvider
一个对象,提供有关区域性特定的格式设置信息 s。
- styles
- DateTimeStyles
枚举值的按位组合,用于定义如何解释与当前时区或当前日期相关的已分析日期。 要指定 None的典型值为 。
- result
- DateTime
此方法返回时,如果 DateTime 转换成功,则包含等效于所 s包含日期和时间的值;如果转换失败,则包含 DateTime.MinValue 。 如果 s 参数为 null空字符串(“”),或不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未初始化传递。