DateTimeOffset.TryParse Method (System) (original) (raw)
Source:
Source:
Source:
Source:
Tries to convert a specified string representation of a date and time to its DateTimeOffset equivalent, and returns a value that indicates whether the conversion succeeded.
public:
static bool TryParse(System::String ^ input, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParse(string input, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParse(string? input, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParse : string * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParse (input As String, formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean
Parameters
input
A string that contains a date and time to convert.
formatProvider
An object that provides culture-specific formatting information about input
.
styles
A bitwise combination of enumeration values that indicates the permitted format of input
.
result
When the method returns, contains the DateTimeOffset value equivalent to the date and time of input
, if the conversion succeeded, or DateTimeOffset.MinValue, if the conversion failed. The conversion fails if the input
parameter is null
or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.
Returns
true
if the input
parameter is successfully converted; otherwise, false
.
Exceptions
Examples
The following example calls the TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) method with a variety of DateTimeStyles values to parse some strings with various date and time formats.
string dateString;
DateTimeOffset parsedDate;
dateString = "05/01/2008 6:00:00";
// Assume time is local
if (DateTimeOffset.TryParse(dateString, null as IFormatProvider,
DateTimeStyles.AssumeLocal,
out parsedDate))
Console.WriteLine("'{0}' was converted to {1}.",
dateString, parsedDate.ToString());
else
Console.WriteLine("Unable to parse '{0}'.", dateString);
// Assume time is UTC
if (DateTimeOffset.TryParse(dateString, null as IFormatProvider,
DateTimeStyles.AssumeUniversal,
out parsedDate))
Console.WriteLine("'{0}' was converted to {1}.",
dateString, parsedDate.ToString());
else
Console.WriteLine("Unable to parse '{0}'.", dateString);
// Parse and convert to UTC
dateString = "05/01/2008 6:00:00AM +5:00";
if (DateTimeOffset.TryParse(dateString, null as IFormatProvider,
DateTimeStyles.AdjustToUniversal,
out parsedDate))
Console.WriteLine("'{0}' was converted to {1}.",
dateString, parsedDate.ToString());
else
Console.WriteLine("Unable to parse '{0}'.", dateString);
// The example displays the following output to the console:
// '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM -07:00.
// '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM +00:00.
// '05/01/2008 6:00:00AM +5:00' was converted to 5/1/2008 1:00:00 AM +00:00.
let dateString = "05/01/2008 6:00:00"
// Assume time is local
match DateTimeOffset.TryParse(dateString, null, DateTimeStyles.AssumeLocal) with
| true, parsedDate ->
printfn $"'{dateString}' was converted to {parsedDate}."
| _ ->
printfn $"Unable to parse '{dateString}'."
// Assume time is UTC
match DateTimeOffset.TryParse(dateString, null, DateTimeStyles.AssumeUniversal) with
| true, parsedDate ->
printfn $"'{dateString}' was converted to {parsedDate}."
| _ ->
printfn $"Unable to parse '{dateString}'."
// Parse and convert to UTC
let dateString = "05/01/2008 6:00:00AM +5:00"
match DateTimeOffset.TryParse(dateString, null, DateTimeStyles.AdjustToUniversal) with
| true, parsedDate ->
printfn $"'{dateString}' was converted to {parsedDate}."
| _ ->
printfn $"Unable to parse '{dateString}'."
// The example displays the following output to the console:
// '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM -07:00.
// '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM +00:00.
// '05/01/2008 6:00:00AM +5:00' was converted to 5/1/2008 1:00:00 AM +00:00.
Dim dateString As String
Dim parsedDate As DateTimeOffset
dateString = "05/01/2008 6:00:00"
' Assume time is local
If DateTimeOffset.TryParse(dateString, Nothing, _
DateTimeStyles.AssumeLocal, _
parsedDate) Then
Console.WriteLine("'{0}' was converted to {1}.", _
dateString, parsedDate.ToString())
Else
Console.WriteLine("Unable to parse '{0}'.", dateString)
End If
' Assume time is UTC
If DateTimeOffset.TryParse(dateString, Nothing, _
DateTimeStyles.AssumeUniversal, _
parsedDate) Then
Console.WriteLine("'{0}' was converted to {1}.", _
dateString, parsedDate.ToString())
Else
Console.WriteLine("Unable to parse '{0}'.", dateString)
End If
' Parse and convert to UTC
dateString = "05/01/2008 6:00:00AM +5:00"
If DateTimeOffset.TryParse(dateString, Nothing, _
DateTimeStyles.AdjustToUniversal, _
parsedDate) Then
Console.WriteLine("'{0}' was converted to {1}.", _
dateString, parsedDate.ToString())
Else
Console.WriteLine("Unable to parse '{0}'.", dateString)
End If
' The example displays the following output to the console:
' '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM -07:00.
' '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM +00:00.
' '05/01/2008 6:00:00AM +5:00' was converted to 5/1/2008 1:00:00 AM +00:00.
Remarks
This overload of the TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) method is like the DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) method, except that it does not throw an exception if the conversion fails. The method parses a string with three elements that can appear in any order and are delimited by white space. These three elements are shown in the following table.
Element | Example |
---|---|
"2/10/2007" | |
"1:02:03 PM" | |
"-7:30" |
Although each of these elements is optional, cannot appear by itself. It must be provided together with either or
The input
string is parsed by using the culture-specific formatting information in a DateTimeFormatInfo object supplied by the formatProvider
parameter. The formatProvider
parameter can be either of the following:
- A CultureInfo object that represents the culture whose formatting is used in
input
. The DateTimeFormatInfo object returned by the CultureInfo.DateTimeFormat property defines the format that is used ininput
. - A DateTimeFormatInfo object that defines the format of date and time data.
In addition, each element can be delimited by leading or trailing white space, and the and
If provider
is null
, the CultureInfo object that corresponds to the current culture is used.
The positive or negative sign used in must be either + or -. It is not defined by the PositiveSign or NegativeSign properties of the NumberFormatInfo object returned by the formatprovider
parameter's NumberFormat property.
The following members of the DateTimeStyles enumeration are supported:
DateTimeStyles Member | Comments |
---|---|
AdjustToUniversal | Parses the string represented by input and, if necessary, converts it to UTC. It is equivalent to parsing a string, and then calling the returned object's ToUniversalTime() method. |
AllowInnerWhite | Although valid, this value is ignored. Inner white space is allowed in the and |
AllowLeadingWhite | Although valid, this value is ignored. Leading white space is allowed in front of each component in the parsed string. |
AllowTrailingWhite | Although valid, this value is ignored. Trailing white space is allowed in front of each component in the parsed string. |
AllowWhiteSpaces | This is the default behavior. It cannot be overridden by supplying a more restrictive DateTimeStyles enumeration value, such as DateTimeStyles.None. |
AssumeLocal | Indicates that, if the input parameter lacks an element, the offset of the local time zone should be provided. This is the default behavior of the TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) method. |
AssumeUniversal | Indicates that, if the input parameter lacks an element, the UTC offset (00:00) should be provided. |
None | Although valid, this value is ignored and has no effect. |
RoundtripKind | Because the DateTimeOffset structure does not include a Kind property, this value has no effect. |
Only the DateTimeStyles.NoCurrentDateDefault value is not supported. An ArgumentException is thrown if this value is included in the styles
parameter.