Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
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
Parameters
A string containing a date and time to convert.
The required format of s
.
An object that supplies culture-specific formatting information about s
.
A bitwise combination of one or more enumeration values that indicate the permitted format of s
.
When this method returns, contains the DateTime value equivalent to the date and time contained in s
, if the conversion succeeded, or DateTime.MinValue if the conversion failed. The conversion fails if either the s
or format
parameter is null
, is an empty string, or does not contain a date and time that correspond to the pattern specified in format
. This parameter is passed uninitialized.
true
if s
was converted successfully; otherwise, false
.
The length of format
is 1, and it is not one of the format specifier characters defined for DateTimeFormatInfo.
-or-
format
does not contain a valid custom format pattern.
The following example demonstrates the DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) method. Note that the string " 5/01/2009 8:30 AM" cannot be parsed successfully when the styles
parameter equals DateTimeStyles.None because leading spaces are not allowed by format
. Additionally, the string "5/01/2009 09:00" cannot be parsed successfully with a format
of "MM/dd/yyyyhh:mm" because the date string does not precede the month number with a leading zero, as format
requires.
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).
Remarks
The DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) method parses the string representation of a date, which must be in the format defined by the format
parameter. It is similar to the DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) method, except that the TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) method does not throw an exception if the conversion fails.
The s
parameter contains the date and time to parse and must be in a format defined by the format
parameter. If date, time, and time zone elements are present in s
, they must also appear in the order specified by format
. If format
defines a date with no time element and the parse operation succeeds, the resulting DateTime value has a time of midnight (00:00:00). If format
defines a time with no date element and the parse operation succeeds, the resulting DateTime value by default has a date of DateTime.Now.Date
, or it has a date of DateTime.MinValue.Date
if styles
includes the DateTimeStyles.NoCurrentDateDefault flag. The style
parameter determines whether the s
parameter can contain leading, inner, or trailing white space characters.
If s
contains no time zone information, the Kind property of the returned DateTime object is DateTimeKind.Unspecified. This behavior can be changed by using the DateTimeStyles.AssumeLocal flag, which returns a DateTime value whose Kind property is DateTimeKind.Local, or by using the DateTimeStyles.AssumeUniversal and DateTimeStyles.AdjustToUniversal flags, which returns a DateTime value whose Kind property is DateTimeKind.Utc. If s contains time zone information, the time is converted to local time, if necessary, and the Kind property of the returned DateTime object is set to DateTimeKind.Local. This behavior can be changed by using the DateTimeStyles.RoundtripKind flag to not convert Coordinated Universal Time (UTC) to a local time and set the Kind property to DateTimeKind.Utc.
The format
parameter contains a pattern that corresponds to the expected format of the s
parameter. The pattern in the format
parameter consists of one or more custom format specifiers from the Custom Date and Time Format Strings table, or a single standard format specifier, which identifies a predefined pattern, from the Standard Date and Time Format Strings table.
If you do not use date or time separators in a custom format pattern, use the invariant culture for the provider
parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the pattern, specify the wider form, "HH", instead of the narrower form, "H".
The particular date and time symbols and strings (such as the names of the days of the week in a particular language) used in s
are defined by the provider
parameter, as is the precise format of s
if format
is a standard format specifier string. The provider
parameter can be any of the following:
A CultureInfo object that represents the culture used to interpret s
. The DateTimeFormatInfo object returned by its DateTimeFormat property defines the symbols and formatting in s
.
A DateTimeFormatInfo object that defines the format of date and time data.
A custom IFormatProvider implementation whose GetFormat method returns either the CultureInfo object or the DateTimeFormatInfo object that provides formatting information.
If provider
is null
, the CultureInfo object that corresponds to the current culture is used.
The styles
parameter includes one or more members of the DateTimeStyles enumeration that determine whether and where white space not defined by format
can appear in s
and that control the precise behavior of the parse operation. The following table describes how each member of the DateTimeStyles enumeration affects the operation of the TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) method.
s
and, if necessary, converts it to UTC. If s
includes a time zone offset, or if s
contains no time zone information but styles
includes the DateTimeStyles.AssumeLocal flag, the method parses the string, calls ToUniversalTime to convert the returned DateTime value to UTC, and sets the Kind property to DateTimeKind.Utc. If s
indicates that it represents UTC, or if s
does not contain time zone information but styles
includes the DateTimeStyles.AssumeUniversal flag, the method parses the string, performs no time zone conversion on the returned DateTime value, and sets the Kind property to DateTimeKind.Utc. In all other cases, the flag has no effect. AllowInnerWhite Specifies that white space not defined by format
can appear between any individual date or time element. AllowLeadingWhite Specifies that white space not defined by format
can appear at the beginning of s
. AllowTrailingWhite Specifies that white space not defined by format
can appear at the end of s
. AllowWhiteSpaces Specifies that s
may contain leading, inner, and trailing white spaces not defined by format
. AssumeLocal Specifies that if s
lacks any time zone information, it is assumed to represent a local time. Unless the DateTimeStyles.AdjustToUniversal flag is present, the Kind property of the returned DateTime value is set to DateTimeKind.Local. AssumeUniversal Specifies that if s
lacks any time zone information, it is assumed to represent UTC. Unless the DateTimeStyles.AdjustToUniversal flag is present, the method converts the returned DateTime value from UTC to local time and sets its Kind property to DateTimeKind.Local. NoCurrentDateDefault If s
contains time without date information, the date of the return value is set to DateTime.MinValue.Date
. None The s
parameter is parsed using default values. No white space other than that present in format
is allowed. If s
lacks a date component, the date of the returned DateTime value is set to 1/1/0001. If s
contains no time zone information, the Kind property of the returned DateTime object is set to DateTimeKind.Unspecified. If time zone information is present in s
, the time is converted to local time and the Kind property of the returned DateTime object is set to DateTimeKind.Local. RoundtripKind For strings that contain time zone information, tries to prevent the conversion to a DateTime value with its Kind property set to DateTimeKind.Local. This flag primarily prevents the conversion of UTC times to local times. Notes to Callers
In the .NET Framework 4, the TryParseExact method returns false
if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. In the .NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4