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)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的格式、区域性特定的格式信息和样式,将日期和时间的指定范围表示形式转换为等效 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 。 如果或sformat参数为null空字符串,或者不包含对应于指定format模式的日期和时间,则转换失败。 此参数未初始化传递。
返回
true 如果 s 已成功转换,则为 :否则为 false。
例外
适用于
TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles, DateTime)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
将日期和时间的指定字符范围转换为其 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。
例外
元素的 formats 长度为 1,它不是为其 DateTimeFormatInfo定义的格式说明符字符之一。
适用于
TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为等效 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 。 如果或sformat参数为null空字符串,或者不包含对应于指定format模式的日期和时间,则转换失败。 此参数未初始化传递。
返回
true 如果 s 已成功转换,则为 :否则为 false。
例外
示例
以下示例演示了该方法 DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) 。 请注意,当参数相等DateTimeStyles.None时,无法成功分析字符串“5/01/2009 8:30 AM”,styles因为前导空格不允许。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,或者其日期DateTime.MinValue.Date为DateTimeStyles.NoCurrentDateDefault包括styles标志。 该 style 参数确定参数是否可以 s 包含前导、内部或尾随空格字符。
如果 s 不包含时区信息, Kind 则 DateTime 返回对象的属性为 DateTimeKind.Unspecified。 可以使用标志来更改DateTimeStyles.AssumeLocal此行为,该标志返回DateTime其Kind属性为DateTimeKind.Local的值,或者通过使用DateTimeStyles.AssumeUniversal和DateTimeStyles.AdjustToUniversal标志来更改此行为,后者返回DateTime其属性为KindDateTimeKind.Utc的值。 如果 s 包含时区信息,则时间将转换为本地时间(如有必要)并将 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 对象。
null如果是provider,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 的值不执行时区转换,并将属性设置为 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 转换为本地时间,并设置其Kind属性为DateTimeKind.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 中,如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 设计器,则 TryParseExact 该方法将返回 false 该方法。 在 .NET Framework 3.5 及更早版本中,将忽略 AM/PM 设计器。
另请参阅
适用于
TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的格式数组、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为等效 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 。 如果或sformats为nulls空字符串,或formats元素的转换失败,或者格式s不是完全由其中formats一种格式模式指定的。 此参数未初始化传递。
返回
例外
元素的 formats 长度为 1,它不是为其 DateTimeFormatInfo定义的格式说明符字符之一。
示例
下面的示例使用 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其Kind属性为DateTimeKind.Local的值,或者通过使用DateTimeStyles.AssumeUniversal和DateTimeStyles.AdjustToUniversal标志来更改此行为,后者返回DateTime其属性为KindDateTimeKind.Utc的值。 如果 s 包含时区信息,则时间将转换为本地时间(如有必要)并将 Kind 返回 DateTime 对象的属性设置为 DateTimeKind.Local。 可以使用标志将协调世界时(UTC)转换为本地时间并将属性设置为 KindDateTimeKind.Utc 来更改DateTimeStyles.RoundtripKind此行为。
该 formats 参数包含模式数组,其中 s 一个模式必须完全匹配(如果分析操作成功)。 参数中的 formats 模式由 自定义日期和时间格式字符串 表中的一个或多个自定义格式说明符组成,或一个标准格式说明符,用于标识 标准日期和时间格式字符串 表中的预定义模式。
如果不在自定义格式模式中使用日期或时间分隔符,请使用参数的固定区域性 provider 和每个自定义格式说明符的最宽格式。 例如,如果要在模式中指定小时数,请指定更宽的窗体“HH”,而不是较窄的窗体“H”。
使用的特定日期和时间符号和字符串(如特定语言中s星期几的名称)由provider参数定义,如标准format格式说明符字符串的精确格式s。 该 provider 参数可以是以下任一参数:
一个 CultureInfo 对象,表示用于解释
s的区域性。 DateTimeFormatInfo其DateTimeFormat属性返回的对象定义符号s和格式。定义 DateTimeFormatInfo 日期和时间数据格式的对象。
一个自定义 IFormatProvider 实现,该方法 GetFormat 返回 CultureInfo 提供格式信息的对象或 DateTimeFormatInfo 对象。
null如果是provider,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 的值不执行时区转换,并将属性设置为 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 转换为本地时间,并设置其Kind属性为DateTimeKind.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 中,如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 设计器,则 TryParseExact 该方法将返回 false 该方法。 在 .NET Framework 3.5 及更早版本中,将忽略 AM/PM 设计器。