DateTime.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将日期和时间的指定字符串表示形式转换为其 DateTime 等效项,并返回一个指示转换是否成功的值。
重载
TryParse(String, IFormatProvider, DateTimeStyles, DateTime) |
使用指定的区域性特定格式信息和格式设置样式,将日期和时间的指定字符串表示形式转换为其 DateTime 等效项,并返回一个指示转换是否成功的值。 |
TryParse(String, IFormatProvider, DateTime) |
尝试将字符串分析为值。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime) |
尝试将字符范围分析为值。 |
TryParse(ReadOnlySpan<Char>, DateTime) |
将日期和时间的指定字符范围转换为其等效的 DateTime,并返回一个指示转换是否成功的值。 |
TryParse(String, DateTime) |
将日期和时间的指定字符串表示形式转换为其 DateTime 等效项,并返回一个指示转换是否成功的值。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime) |
使用指定的区域性特定格式信息和格式设置样式,将日期和时间的范围表示形式转换为其等效的 DateTime,并返回一个指示转换是否成功的值。 |
注解
重要
日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendar 和 JapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息以及如何确定应用程序是否受影响,请参阅在 .net 中的日式日历中处理新时代。 若要了解如何在 Windows 系统上测试应用程序以确保其应用程序更改的就绪性,请参阅准备应用程序以进行日本时代更改。 对于 .NET 中支持多个纪元的日历的功能,以及在处理支持多个纪元的日历时的最佳做法,请参阅使用 纪元。
TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
使用指定的区域性特定格式信息和格式设置样式,将日期和时间的指定字符串表示形式转换为其 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
此方法返回时,如果转换成功,则包含等效于所s
包含日期和时间的值;如果转换失败,则包含 DateTime DateTime.MinValue。 如果 s
参数为 null
,是空字符串 ("") 或者不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
参数成功转换,则为 s
;否则为 false
。
例外
styles
不是有效的 DateTimeStyles 值。
- 或 -
styles
包含无效的 DateTimeStyles 值组合(例如,AssumeLocal 和 AssumeUniversal)。
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 用作默认时间。 如果某个日期存在,但其年份组件仅包含两位数字,则根据属性的值Calendar.TwoDigitYearMax将参数的当前日历中的provider
年份转换为年份。 将忽略任何前导、内部或尾随空格字符 s
。 日期和时间可以用前导和尾随 NUMBER SIGN 字符 ('#'、U+0023) 的配对括号括起来,并且可以用一个或多个 NULL 字符 (U+0000) 尾随。
日期和时间元素以及日期和时间中使用的名称和符号的特定有效格式由 provider
参数定义,可以是下列任一项:
一个 CultureInfo 对象,表示在参数中使用其格式的
s
区域性。 属性 DateTimeFormatInfo 返回 CultureInfo.DateTimeFormat 的对象定义在 . 中使用的s
格式设置。定义 DateTimeFormatInfo 在 . 中使用的
s
格式的对象。一个自定义 IFormatProvider 实现。 Its IFormatProvider.GetFormat method returns a DateTimeFormatInfo object that defines the formatting used in
s
.
如果 provider
为 null
,则使用当前区域性。
如果 s
当前日历中的 leap 年份中的 leap 日是字符串表示形式,则该方法已成功分析 s
。 如果是 s
当前日历 provider
中非 leap 年份中的 leap 日字符串表示形式,则分析操作将失败,并且该方法返回 false
。
该 styles
参数定义分析字符串的精确解释,以及分析操作应如何处理它。 它可以是枚举的 DateTimeStyles 一个或多个成员,如下表所述。
DateTimeStyles 成员 | 说明 |
---|---|
AdjustToUniversal | s 分析,如有必要,将其转换为 UTC。 如果s 包含时区偏移量,或者如果s 不包含时区信息,但styles 包含DateTimeStyles.AssumeLocal标志,该方法将分析字符串,调用ToUniversalTime将返回DateTime的值转换为 UTC,并将属性DateTimeKind.Utc设置为 Kind 。 如果 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。 |
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.UtcUTC。
可以使用标志重写 DateTimeStyles.RoundtripKind 此行为。
分析自定义区域性
如果分析为自定义区域性生成的日期和时间字符串,请使用 TryParseExact 该方法而不是 TryParse 方法来提高分析操作成功的可能性。 自定义区域性日期和时间字符串可能复杂且难以分析。 该方法 TryParse 尝试分析具有多个隐式分析模式的字符串,所有这些模式都可能失败。 相反,该方法 TryParseExact 要求你显式指定一个或多个可能成功的确切分析模式。
有关自定义区域性的详细信息,请参阅 System.Globalization.CultureAndRegionInfoBuilder 该类。
调用方说明
格式受当前 DateTimeFormatInfo 对象的属性的影响,该属性由 provider
参数提供。 如果当前DateSeparator和属性设置为相同的值,该方法TryParse可能会意外失败并TimeSeparator返回False
。
另请参阅
适用于
TryParse(String, IFormatProvider, DateTime)
尝试将字符串分析为值。
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
- result
- DateTime
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime)
尝试将字符范围分析为值。
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
- result
- DateTime
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Char>, DateTime)
将日期和时间的指定字符范围转换为其等效的 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
此方法返回时,如果转换成功,则包含等效于所s
包含日期和时间的值;如果转换失败,则包含 DateTime DateTime.MinValue。 如果 s
参数为 null
,是空字符串 ("") 或者不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
参数成功转换,则为 s
;否则为 false
。
适用于
TryParse(String, DateTime)
将日期和时间的指定字符串表示形式转换为其 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
此方法返回时,如果转换成功,则包含等效于所s
包含日期和时间的值;如果转换失败,则包含 DateTime DateTime.MinValue。 如果 s
参数为 null
,是空字符串 ("") 或者不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
参数成功转换,则为 s
;否则为 false
。
示例
以下示例将多个日期和时间字符串传递给该方法 DateTime.TryParse(String, DateTime) 。
using namespace System;
using namespace System::Globalization;
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);
else
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);
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
。 日期和时间可以用前导和尾随 NUMBER SIGN 字符 ('#'、U+0023) 的配对括号括起来,并且可以用一个或多个 NULL 字符 (U+0000) 尾随。
由于该方法 DateTime.TryParse(String, DateTime) 尝试使用当前区域性的格式规则分析日期和时间的字符串表示形式,因此尝试分析不同区域性中的特定字符串可能会失败或返回不同的结果。 如果特定日期和时间格式将跨不同的区域设置进行分析,请使用 DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) 该方法或方法的 TryParseExact 重载之一并提供格式说明符。
如果 s
当前日历中的 leap 年份中的 leap 日是字符串表示形式,则该方法已成功分析 s
。 如果 s
当前区域性的当前日历中的非 leap 年份中的 leap 日是一个字符串表示形式,则分析操作将失败,并且该方法返回 false
。
如果s
不包含时区信息,result
则包含其DateTime属性为DateTimeKind.Unspecified方法返回时的值Kind。 如果要分析的字符串包含时区信息,result
则包含其DateTimeKind属性为DateTimeKind.Local方法返回的值。
调用方说明
格式受当前DateTimeFormatInfo对象的属性的影响,该属性默认派生自控制面板中的 “区域和语言选项” 项。 如果当前DateSeparator和属性设置为相同的值,该方法TryParse可能会意外失败并TimeSeparator返回False
。
另请参阅
- Parse
- CultureInfo
- DateTimeFormatInfo
- 在 .NET Framework 中分析日期和时间字符串
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串
- 示例:.NET Core WinForms 格式设置实用工具 (C#)
- 示例:.NET Core WinForms 格式设置实用工具 (Visual Basic)
适用于
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)
使用指定的区域性特定格式信息和格式设置样式,将日期和时间的范围表示形式转换为其等效的 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
此方法返回时,如果转换成功,则包含等效于所s
包含日期和时间的值;如果转换失败,则包含 DateTime DateTime.MinValue。 如果 s
参数为 null
,是空字符串 ("") 或者不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
参数成功转换,则为 s
;否则为 false
。