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,并返回一个指示转换是否成功的值。 |
注解
重要
日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendar 和 JapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其为时代更改做好准备的信息,请参阅 为日本时代更改准备应用程序。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元。
TryParse(ReadOnlySpan<Char>, DateTime)
- 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
,是空字符串 ("") 或者不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
参数成功转换,则为 s
;否则为 false
。
适用于
TryParse(String, DateTime)
- 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
,是空字符串 ("") 或者不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未经初始化即被传递。
返回
如果 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
是当前日历中闰年中闰日的字符串表示形式,则 该方法将成功分析 s
。 如果 s
是当前区域性当前日历中非闰年中闰日的字符串表示形式,则分析操作失败,方法返回 false
。
如果s
不包含时区信息,则包含一个DateTime值,result
其Kind属性在DateTimeKind.Unspecified方法返回时为 。 如果要分析的字符串包含时区信息,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, DateTime)
- 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
的结果或失败时的未定义值。
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(String, IFormatProvider, DateTime)
- 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
的结果或失败时的未定义值。
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
- 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
,是空字符串 ("") 或者不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
参数成功转换,则为 s
;否则为 false
。
例外
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.
注解
有关此 API 的详细信息,请参阅 DateTime.TryParse 的补充 API 说明。
调用方说明
格式设置受当前 DateTimeFormatInfo 对象的属性的影响,该属性由 provider
参数提供。 如果当前 DateSeparator 和 属性设置为相同的值,方法TryParse可能会意外失败并TimeSeparator返回 False
。
另请参阅
适用于
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)
- 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
,是空字符串 ("") 或者不包含日期和时间的有效字符串表示形式,则转换失败。 此参数未经初始化即被传递。
返回
如果 true
参数成功转换,则为 s
;否则为 false
。