DateTime.TryParseExact 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将日期和时间的指定字符串表示形式转换为其等效的 DateTime。 字符串表示形式的格式必须与指定的格式完全匹配。 该方法返回一个指示转换是否成功的值。
重载
TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime) |
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定范围表示形式转换为其等效的 DateTime。 字符串表示形式的格式必须与指定的格式完全匹配。 该方法返回一个指示转换是否成功的值。 |
TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles, DateTime) |
将日期和时间的指定字符范围转换为其等效的 DateTime,并返回一个指示转换是否成功的值。 |
TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) |
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTime。 字符串表示形式的格式必须与指定的格式完全匹配。 该方法返回一个指示转换是否成功的值。 |
TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) |
使用指定的格式数组、区域性特定格式信息和样式,将日期和时间的指定字符串表示形式转换为其等效的 DateTime。 字符串表示形式的格式必须至少与指定的格式之一完全匹配。 该方法返回一个指示转换是否成功的值。 |
注解
重要
日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendar 和 JapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其准备好进行纪元更改的信息,请参阅 准备应用程序应对日本纪元更改。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元。
TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定范围表示形式转换为其等效的 DateTime。 字符串表示形式的格式必须与指定的格式完全匹配。 该方法返回一个指示转换是否成功的值。
public:
static bool TryParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider ^ provider, System::Globalization::DateTimeStyles style, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParseExact (ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider? provider, System.Globalization.DateTimeStyles style, out DateTime result);
public static bool TryParseExact (ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider provider, System.Globalization.DateTimeStyles style, out DateTime result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles * DateTime -> bool
Public Shared Function TryParseExact (s As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), provider As IFormatProvider, style As DateTimeStyles, ByRef result As DateTime) As Boolean
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的日期和时间的字符。
- format
- ReadOnlySpan<Char>
所需的 s
格式。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- style
- DateTimeStyles
一个或多个枚举值的按位组合,指示 s
允许使用的格式。
- result
- DateTime
此方法返回时,包含 DateTime 与 中包含的 s
日期和时间等效的值(如果转换成功),如果转换失败,则包含 DateTime.MinValue 。 如果 s
或 format
参数为 null
,或者为空字符串,或者未包含对应于 format
中指定的模式的日期和时间,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
成功转换,则为 s
;否则为 false
。
适用于
TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles, DateTime)
将日期和时间的指定字符范围转换为其等效的 DateTime,并返回一个指示转换是否成功的值。
public:
static bool TryParseExact(ReadOnlySpan<char> s, cli::array <System::String ^> ^ formats, IFormatProvider ^ provider, System::Globalization::DateTimeStyles style, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParseExact (ReadOnlySpan<char> s, string?[]? formats, IFormatProvider? provider, System.Globalization.DateTimeStyles style, out DateTime result);
public static bool TryParseExact (ReadOnlySpan<char> s, string[] formats, IFormatProvider provider, System.Globalization.DateTimeStyles style, out DateTime result);
static member TryParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.DateTimeStyles * DateTime -> bool
Public Shared Function TryParseExact (s As ReadOnlySpan(Of Char), formats As String(), provider As IFormatProvider, style As DateTimeStyles, ByRef result As DateTime) As Boolean
参数
- s
- ReadOnlySpan<Char>
一个范围,包含要分析的字符串。
- formats
- String[]
s
的允许格式的数组。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- style
- DateTimeStyles
枚举值的按位组合,该组合定义如何根据当前时区或当前日期解释已分析日期。 要指定的一个典型值为 None。
- result
- DateTime
此方法返回时,包含 DateTime 与 中包含的 s
日期和时间等效的值(如果转换成功),如果转换失败,则包含 DateTime.MinValue 。 如果 s
参数为 null
,为 Empty,或者不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
参数成功转换,则为 s
;否则为 false
。
适用于
TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime)
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTime。 字符串表示形式的格式必须与指定的格式完全匹配。 该方法返回一个指示转换是否成功的值。
public:
static bool TryParseExact(System::String ^ s, System::String ^ format, IFormatProvider ^ provider, System::Globalization::DateTimeStyles style, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParseExact (string s, string format, IFormatProvider provider, System.Globalization.DateTimeStyles style, out DateTime result);
public static bool TryParseExact (string? s, string? format, IFormatProvider? provider, System.Globalization.DateTimeStyles style, out DateTime result);
static member TryParseExact : string * string * IFormatProvider * System.Globalization.DateTimeStyles * DateTime -> bool
Public Shared Function TryParseExact (s As String, format As String, provider As IFormatProvider, style As DateTimeStyles, ByRef result As DateTime) As Boolean
参数
- s
- String
包含要转换的日期和时间的字符串。
- format
- String
所需的 s
格式。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- style
- DateTimeStyles
一个或多个枚举值的按位组合,指示 s
允许使用的格式。
- result
- DateTime
此方法返回时,包含 DateTime 与 中包含的 s
日期和时间等效的值(如果转换成功),如果转换失败,则包含 DateTime.MinValue 。 如果 s
或 format
参数为 null
,或者为空字符串,或者未包含对应于 format
中指定的模式的日期和时间,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
成功转换,则为 s
;否则为 false
。
例外
示例
下面的示例演示 DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) 方法。 请注意,当 参数相等DateTimeStyles.None时styles
,无法成功分析字符串“5/01/2009 8:30 AM”,因为 不允许format
前导空格。 此外,字符串“5/01/2009 09:00”不能成功解析为 format
“MM/dd/yyyyhh:mm”的字符串,因为日期字符串在月份数字之前没有前导零,根据需要 format
。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo enUS = new CultureInfo("en-US");
string dateString;
DateTime dateValue;
// Parse date with no style flags.
dateString = " 5/01/2009 8:30 AM";
if (DateTime.TryParseExact(dateString, "g", enUS,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Allow a leading space in the date string.
if (DateTime.TryParseExact(dateString, "g", enUS,
DateTimeStyles.AllowLeadingWhite, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Use custom formats with M and MM.
dateString = "5/01/2009 09:00";
if (DateTime.TryParseExact(dateString, "M/dd/yyyy hh:mm", enUS,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Allow a leading space in the date string.
if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm", enUS,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Parse a string with time zone information.
dateString = "05/01/2009 01:30:42 PM -05:00";
if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Allow a leading space in the date string.
if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS,
DateTimeStyles.AdjustToUniversal, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Parse a string representing UTC.
dateString = "2008-06-11T16:11:20.0904778Z";
if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
}
// The example displays the following output:
// ' 5/01/2009 8:30 AM' is not in an acceptable format.
// Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
// Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
// '5/01/2009 09:00' is not in an acceptable format.
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
open System
open System.Globalization
[<EntryPoint>]
let main _ =
let enUS = CultureInfo "en-US"
// Parse date with no style flags.
let dateString = " 5/01/2009 8:30 AM"
match DateTime.TryParseExact(dateString, "g", enUS, DateTimeStyles.None) with
| true, dateValue ->
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $"'{dateString}' is not in an acceptable format."
// Allow a leading space in the date string.
match DateTime.TryParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite) with
| true, dateValue ->
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $"'{dateString}' is not in an acceptable format."
// Use custom formats with M and MM.
let dateString = "5/01/2009 09:00"
match DateTime.TryParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None) with
| true, dateValue ->
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $"'{dateString}' is not in an acceptable format."
// Allow a leading space in the date string.
match DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None) with
| true, dateValue ->
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $"'{dateString}' is not in an acceptable format."
// Parse a string with time zone information.
let dateString = "05/01/2009 01:30:42 PM -05:00"
match DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None) with
| true, dateValue ->
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $"'{dateString}' is not in an acceptable format."
// Allow a leading space in the date string.
match DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.AdjustToUniversal) with
| true, dateValue ->
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $"'{dateString}' is not in an acceptable format."
// Parse a string representing UTC.
let dateString = "2008-06-11T16:11:20.0904778Z"
match DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, DateTimeStyles.None) with
| true, dateValue ->
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $"'{dateString}' is not in an acceptable format."
match DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind) with
| true, dateValue ->
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
| _ ->
printfn $"'{dateString}' is not in an acceptable format."
0
// The example displays the following output:
// ' 5/01/2009 8:30 AM' is not in an acceptable format.
// Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
// Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
// '5/01/2009 09:00' is not in an acceptable format.
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim enUS As New CultureInfo("en-US")
Dim dateString As String
Dim dateValue As Date
' Parse date with no style flags.
dateString = " 5/01/2009 8:30 AM"
If Date.TryParseExact(dateString, "g", enUS, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Allow a leading space in the date string.
If Date.TryParseExact(dateString, "g", enUS, _
DateTimeStyles.AllowLeadingWhite, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Use custom formats with M and MM.
dateString = "5/01/2009 09:00"
If Date.TryParseExact(dateString, "M/dd/yyyy hh:mm", enUS, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Allow a leading space in the date string.
If Date.TryParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Parse a string with time zone information.
dateString = "05/01/2009 01:30:42 PM -05:00"
If Date.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Allow a leading space in the date string.
If Date.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, _
DateTimeStyles.AdjustToUniversal, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Parse a string representing UTC.
dateString = "2008-06-11T16:11:20.0904778Z"
If Date.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
If Date.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, _
DateTimeStyles.RoundtripKind, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
End Sub
End Module
' The example displays the following output:
' ' 5/01/2009 8:30 AM' is not in an acceptable format.
' Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
' Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
' '5/01/2009 09:00' is not in an acceptable format.
' Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
' Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
' Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
' Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
注解
方法 DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) 分析日期的字符串表示形式,日期必须采用 参数 format
定义的格式。 它类似于 DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) 方法,只是方法 TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) 不会在转换失败时引发异常。
参数 s
包含要分析的日期和时间,并且必须采用 参数 format
定义的格式。 如果日期、时间和时区元素存在于 中 s
,则它们还必须按 指定的 format
顺序显示。 如果 format
定义了没有时间元素的日期,并且分析操作成功,则生成的 DateTime 值为午夜 (00:00:00) 。 如果format
定义了一个没有日期元素的时间,并且分析操作成功,则默认生成的DateTime值为 的日期DateTime.Now.Date
,或者如果styles
包含 标志,DateTimeStyles.NoCurrentDateDefault则其日期DateTime.MinValue.Date
为 。 参数 style
确定 s
参数可以包含前导、内部或尾随空格字符。
如果 s
不包含时区信息,则 Kind 返回 DateTime 的 对象的 属性为 DateTimeKind.Unspecified。 可以使用 标志来更改DateTimeStyles.AssumeLocal此行为,该标志返回DateTime其属性为 DateTimeKind.Local的值Kind,或使用 DateTimeStyles.AssumeUniversal 和 DateTimeStyles.AdjustToUniversal 标志,后者返回DateTime其 属性为 DateTimeKind.Utc的值Kind。 如果 包含时区信息,则如有必要,时间将转换为本地时间,并且 Kind 返回 DateTime 对象的 属性设置为 DateTimeKind.Local。 可以使用 标志不将协调世界时 (UTC) 转换为本地时间,并将 属性设置为 KindDateTimeKind.Utc来更改DateTimeStyles.RoundtripKind此行为。
参数 format
包含对应于参数的预期格式的 s
模式。 参数中的 format
模式由自定义 日期和时间格式字符串 表中的一个或多个自定义格式说明符或单个标准格式说明符组成,用于标识 标准日期和时间格式字符串 表中的预定义模式。
如果不在自定义格式模式中使用日期或时间分隔符,请使用参数的固定区域性 provider
和每个自定义格式说明符的最宽格式。 例如,如果要在模式中指定小时数,请指定较宽的窗体“HH”,而不是较窄的窗体“H”。
注意
可以调用 DateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) 方法并s
指定多种允许的格式,而不是要求符合单个格式才能使分析操作成功。 这使得分析操作更有可能成功。
(特定日期和时间符号和字符串(例如特定语言) 使用的s
星期几的名称)由 provider
参数定义,如果 format
是标准格式说明符字符串,则为 的精确格式s
。 参数 provider
可以是以下任一参数:
一个 CultureInfo 对象,表示用于解释
s
的区域性。 DateTimeFormatInfo其 DateTimeFormat 属性返回的对象定义 中的s
符号和格式。一个 DateTimeFormatInfo 定义日期和时间数据格式的 对象。
一个自定义 IFormatProvider 实现,其 GetFormat 方法返回 CultureInfo 提供格式设置信息的 对象或 DateTimeFormatInfo 对象。
如果 provider
为 null
,则 CultureInfo 使用与当前区域性对应的 对象。
参数 styles
包括枚举的 DateTimeStyles 一个或多个成员,这些成员确定未定义的 format
空白是否和在哪里可以显示 s
,以及控制分析操作的精确行为。 下表描述了 枚举的每个成员 DateTimeStyles 如何影响 方法的操作 TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) 。
DateTimeStyles 成员 | 说明 |
---|---|
AdjustToUniversal | 分析 s 并在必要时将其转换为 UTC。 如果 s 包含时区偏移量,或者如果 s 不包含时区信息但 styles 包含 DateTimeStyles.AssumeLocal 标志,则 方法将分析字符串,调用 ToUniversalTime 以将返回 DateTime 的值转换为 UTC,并将 属性 Kind 设置为 DateTimeKind.Utc。 如果 s 指示它表示 UTC,或者如果 s 不包含时区信息但 styles 包含 DateTimeStyles.AssumeUniversal 标志,则 方法将分析字符串,对返回 DateTime 的值不执行时区转换,并将 属性设置为 KindDateTimeKind.Utc。 在所有其他情况下,标志不起作用。 |
AllowInnerWhite | 指定未定义的 format 空格可以在任何单个日期或时间元素之间显示。 |
AllowLeadingWhite | 指定未定义的 format 空格可以出现在 的 s 开头。 |
AllowTrailingWhite | 指定 未定义的 format 空白可以出现在 的 s 末尾。 |
AllowWhiteSpaces | 指定可能包含 s 未定义的 format 前导空格、内部空格和尾随空格。 |
AssumeLocal | 指定如果 s 缺少任何时区信息,则假定它表示本地时间。 DateTimeStyles.AdjustToUniversal除非存在 标志,Kind否则返回DateTime值的 属性设置为 DateTimeKind.Local。 |
AssumeUniversal | 指定如果 s 缺少任何时区信息,则假定它表示 UTC。 DateTimeStyles.AdjustToUniversal除非存在 标志,否则 该方法将返回DateTime的值从 UTC 转换为本地时间,并将其 属性设置为 KindDateTimeKind.Local。 |
NoCurrentDateDefault | 如果 s 包含不含日期信息的时间,则返回值的日期设置为 DateTime.MinValue.Date 。 |
None | 参数 s 是使用默认值进行分析的。 除 中 format 存在的空格外,不允许使用空格。 如果 s 缺少日期组件,则返回 DateTime 值的日期设置为 1/1/0001。 如果 s 不包含时区信息,则 Kind 返回 DateTime 对象的 属性设置为 DateTimeKind.Unspecified。 如果 时区信息存在于 中 s ,则时间将转换为本地时间, Kind 并且返回 DateTime 的 对象的 属性设置为 DateTimeKind.Local。 |
RoundtripKind | 对于包含时区信息的字符串, 尝试阻止 DateTime 转换为其 Kind 属性设置为 DateTimeKind.Local的值。 此标志主要阻止将 UTC 时间转换为本地时间。 |
调用方说明
在.NET Framework 4 中,TryParseExact如果要分析的字符串包含一个小时分量和一个不一致 AM/PM 指示符,则方法将返回 false
。 在 .NET Framework 3.5 及更早版本中,将忽略 AM/PM 指示符。
另请参阅
适用于
TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)
使用指定的格式数组、区域性特定格式信息和样式,将日期和时间的指定字符串表示形式转换为其等效的 DateTime。 字符串表示形式的格式必须至少与指定的格式之一完全匹配。 该方法返回一个指示转换是否成功的值。
public:
static bool TryParseExact(System::String ^ s, cli::array <System::String ^> ^ formats, IFormatProvider ^ provider, System::Globalization::DateTimeStyles style, [Runtime::InteropServices::Out] DateTime % result);
public static bool TryParseExact (string s, string[] formats, IFormatProvider provider, System.Globalization.DateTimeStyles style, out DateTime result);
public static bool TryParseExact (string? s, string?[]? formats, IFormatProvider? provider, System.Globalization.DateTimeStyles style, out DateTime result);
static member TryParseExact : string * string[] * IFormatProvider * System.Globalization.DateTimeStyles * DateTime -> bool
Public Shared Function TryParseExact (s As String, formats As String(), provider As IFormatProvider, style As DateTimeStyles, ByRef result As DateTime) As Boolean
参数
- s
- String
包含要转换的日期和时间的字符串。
- formats
- String[]
s
的允许格式的数组。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式信息。
- style
- DateTimeStyles
枚举值的一个按位组合,指示 s
所允许的格式。 要指定的一个典型值为 None。
- result
- DateTime
此方法返回时,如果转换成功,则 DateTime 包含与 中包含的 s
日期和时间等效的值;如果转换失败,则包含 DateTime.MinValue 。 如果 s
或 formats
为 null
,s
或 formats
的某个元素为空字符串, 或者 s
的格式与 formats
中的格式模式所指定的格式都不完全匹配,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
参数成功转换,则为 s
;否则为 false
。
例外
示例
以下示例使用 DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) 方法确保可以成功分析采用多种可能格式的字符串。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"};
string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM",
"5/1/2009 6:32:00", "05/01/2009 06:32",
"05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"};
DateTime dateValue;
foreach (string dateString in dateStrings)
{
if (DateTime.TryParseExact(dateString, formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out dateValue))
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
else
Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
}
}
}
// The example displays the following output:
// Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
// Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
// Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
// Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
// Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
// Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
open System
open System.Globalization
let formats=
[| "M/d/yyyy h:mm:ss tt"; "M/d/yyyy h:mm tt"
"MM/dd/yyyy hh:mm:ss"; "M/d/yyyy h:mm:ss"
"M/d/yyyy hh:mm tt"; "M/d/yyyy hh tt"
"M/d/yyyy h:mm"; "M/d/yyyy h:mm"
"MM/dd/yyyy hh:mm"; "M/dd/yyyy hh:mm" |]
let dateStrings =
[ "5/1/2009 6:32 PM"; "05/01/2009 6:32:05 PM"
"5/1/2009 6:32:00"; "05/01/2009 06:32"
"05/01/2009 06:32:00 PM"; "05/01/2009 06:32:00" ]
for dateString in dateStrings do
match DateTime.TryParseExact(dateString, formats, CultureInfo "en-US", DateTimeStyles.None) with
| true, dateValue ->
printfn $"Converted '{dateString}' to {dateValue}."
| _ ->
printfn $"Unable to convert '{dateString}' to a date."
// The example displays the following output:
// Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
// Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
// Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
// Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
// Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
// Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim formats() As String = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", _
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", _
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", _
"M/d/yyyy h:mm", "M/d/yyyy h:mm", _
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"}
Dim dateStrings() As String = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", _
"5/1/2009 6:32:00", "05/01/2009 06:32", _
"05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"}
Dim dateValue As DateTime
For Each dateString As String In dateStrings
If Date.TryParseExact(dateString, formats, _
New CultureInfo("en-US"), _
DateTimeStyles.None, _
dateValue) Then
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue)
Else
Console.WriteLine("Unable to convert '{0}' to a date.", dateString)
End If
Next
End Sub
End Module
' The example displays the following output:
' Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
' Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
' Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
' Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
' Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
' Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
注解
方法 DateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) 分析与分配给 formats
参数的任何一种模式匹配的日期的字符串表示形式。 它类似于 DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 方法,只是 方法 TryParseExact 不会在转换失败时引发异常。
参数 s
包含要分析的日期和时间。 s
如果 参数仅包含一个时间且不包含日期,则使用当前日期,除非参数style
包含 DateTimeStyles.NoCurrentDateDefault 标志,在这种情况下,将使用默认日期 (DateTime.Date.MinValue
) 。 s
如果 参数仅包含日期且不包含时间,则使用午夜 (00:00:00) 。 参数 style
还确定 s
参数是否可以包含中某个格式字符串 formats
所允许的空格字符以外的前导字符、内部空格字符或尾随空格字符。
如果 s
不包含时区信息,则 Kind 返回 DateTime 的 对象的 属性为 DateTimeKind.Unspecified。 可以使用 标志更改DateTimeStyles.AssumeLocal此行为,该标志返回DateTime属性为 DateTimeKind.Local的值Kind,或者使用 DateTimeStyles.AssumeUniversal 和 DateTimeStyles.AdjustToUniversal 标志,后者返回DateTime其 属性为 DateTimeKind.Utc的值Kind。 如果 包含时区信息,则时间将转换为本地时间(如有必要),并且 Kind 返回 DateTime 对象的 属性设置为 DateTimeKind.Local。 可以使用 标志更改此行为, DateTimeStyles.RoundtripKind 以便不将协调世界时 (UTC) 转换为本地时间, Kind 并将 属性设置为 DateTimeKind.Utc。
参数 formats
包含模式数组,如果分析操作要成功,其中 s
一个模式必须完全匹配。 参数中的 formats
模式由“ 自定义日期和时间格式字符串 ”表中的一个或多个自定义格式说明符,或“标准日期和时间格式字符串”表中标识预定义模式 的单个标准格式 说明符组成。
如果不在自定义格式模式中使用日期或时间分隔符,请使用参数的固定区域性 provider
和每个自定义格式说明符的最宽格式。 例如,如果要在 模式中指定小时,请指定更宽的格式“HH”,而不是较窄的形式“H”。
特定日期和时间符号以及 (字符串(如特定语言) 使用的s
星期几的名称)由 provider
参数定义,如果 format
是标准格式说明符字符串,则为 的精确格式s
。 参数 provider
可以是以下任一项:
一个 CultureInfo 对象,表示用于解释
s
的区域性。 DateTimeFormatInfo其 DateTimeFormat 属性返回的对象定义 中的s
符号和格式。一个 DateTimeFormatInfo 定义日期和时间数据格式的 对象。
一个自定义 IFormatProvider 实现,其 GetFormat 方法返回 CultureInfo 对象或 DateTimeFormatInfo 提供格式设置信息的 对象。
如果 provider
为 null
,则 CultureInfo 使用与当前区域性对应的 对象。
参数 styles
包含枚举的 DateTimeStyles 一个或多个成员,这些成员确定未定义的 format
空格是否和位置可以出现在其中 s
,以及控制分析操作的精确行为。 下表描述了 枚举的每个成员 DateTimeStyles 如何影响 方法的操作 TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) 。
DateTimeStyles 成员 | 说明 |
---|---|
AdjustToUniversal | 分析 s 并在必要时将其转换为 UTC。 如果 s 包含时区偏移量,或者如果 s 不包含时区信息但 styles 包含 DateTimeStyles.AssumeLocal 标志,该方法将分析字符串,调用 ToUniversalTime 以将返回 DateTime 的值转换为 UTC,并将 属性设置为 KindDateTimeKind.Utc。 如果 s 指示它表示 UTC,或者如果 s 不包含时区信息但 styles 包含 DateTimeStyles.AssumeUniversal 标志,则 该方法将分析字符串,不对返回 DateTime 的值执行时区转换,并将 属性 Kind 设置为 DateTimeKind.Utc。 在所有其他情况下,标志不起作用。 |
AllowInnerWhite | 指定未定义的 format 空格可以在任何单个日期或时间元素之间显示。 |
AllowLeadingWhite | 指定 未定义的 format 空白可以出现在 的 s 开头。 |
AllowTrailingWhite | 指定 未定义的 format 空白可以出现在 的 s 末尾。 |
AllowWhiteSpaces | 指定 可能包含 s 未定义的 format 前导空格、内部空格和尾随空格。 |
AssumeLocal | 指定如果 s 缺少任何时区信息,则假定它表示本地时间。 DateTimeStyles.AdjustToUniversal除非存在 标志,Kind否则返回DateTime值的 属性设置为 DateTimeKind.Local。 |
AssumeUniversal | 指定如果 s 缺少任何时区信息,则假定它表示 UTC。 DateTimeStyles.AdjustToUniversal除非存在 标志,否则 该方法将返回DateTime的值从 UTC 转换为本地时间,并将其 属性设置为 KindDateTimeKind.Local。 |
NoCurrentDateDefault | 如果 s 包含不含日期信息的时间,则返回值的日期设置为 DateTime.MinValue.Date 。 |
None | 参数 s 是使用默认值进行分析的。 除 中 format 存在的空格外,不允许使用空格。 如果 s 缺少日期组件,则返回 DateTime 值的日期设置为 1/1/0001。 如果 s 不包含时区信息,则 Kind 返回 DateTime 对象的 属性设置为 DateTimeKind.Unspecified。 如果 时区信息存在于 中 s ,则时间将转换为本地时间, Kind 并且返回 DateTime 的 对象的 属性设置为 DateTimeKind.Local。 |
RoundtripKind | 对于包含时区信息的字符串, 尝试阻止 DateTime 转换为其 Kind 属性设置为 DateTimeKind.Local的值。 此标志主要阻止将 UTC 时间转换为本地时间。 |
调用方说明
在.NET Framework 4 中,TryParseExact如果要分析的字符串包含小时分量和不一致 AM/PM 指示符,则方法返回 false
。 在 .NET Framework 3.5 及更早版本中,将忽略 AM/PM 指示符。