Muokkaa

Jaa


DateTimeOffset.Parse Method

Definition

Converts the specified string representation of a date and time to its DateTimeOffset equivalent.

Overloads

Parse(String)

Converts the specified string representation of a date, time, and offset to its DateTimeOffset equivalent.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Parses a span of characters into a value.

Parse(String, IFormatProvider)

Converts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified culture-specific format information.

Parse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

Converts the specified span representation of a date and time to its DateTimeOffset equivalent using the specified culture-specific format information and formatting style.

Parse(String, IFormatProvider, DateTimeStyles)

Converts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified culture-specific format information and formatting style.

Remarks

Important

Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. For example, May 1, 2019 marked the beginning of the Reiwa era in the JapaneseCalendar and JapaneseLunisolarCalendar. Such a change of era affects all applications that use these calendars. For more information and to determine whether your applications are affected, see Handling a new era in the Japanese calendar in .NET. For information on testing your applications on Windows systems to ensure their readiness for the era change, see Prepare your application for the Japanese era change. For features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras, see Working with eras.

Parse(String)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Converts the specified string representation of a date, time, and offset to its DateTimeOffset equivalent.

public:
 static DateTimeOffset Parse(System::String ^ input);
public static DateTimeOffset Parse (string input);
static member Parse : string -> DateTimeOffset
Public Shared Function Parse (input As String) As DateTimeOffset

Parameters

input
String

A string that contains a date and time to convert.

Returns

An object that is equivalent to the date and time that is contained in input.

Exceptions

The offset is greater than 14 hours or less than -14 hours.

input is null.

input does not contain a valid string representation of a date and time.

-or-

input contains the string representation of an offset value without a date or time.

Examples

The following example calls the Parse(String) method to parse several date and time strings. The example includes output from March 22, 2007, on a system whose culture is en-us.

string dateString;
DateTimeOffset offsetDate;

// String with date only
dateString = "05/01/2008";
offsetDate = DateTimeOffset.Parse(dateString);
Console.WriteLine(offsetDate.ToString());

// String with time only
dateString = "11:36 PM";
offsetDate = DateTimeOffset.Parse(dateString);
Console.WriteLine(offsetDate.ToString());

// String with date and offset
dateString = "05/01/2008 +1:00";
offsetDate = DateTimeOffset.Parse(dateString);
Console.WriteLine(offsetDate.ToString());

// String with day abbreviation
dateString = "Thu May 01, 2008";
offsetDate = DateTimeOffset.Parse(dateString);
Console.WriteLine(offsetDate.ToString());
// String with date only
let dateString = "05/01/2008"
let offsetDate = DateTimeOffset.Parse dateString
printfn $"{offsetDate}"

// String with time only
let dateString = "11:36 PM"
let offsetDate = DateTimeOffset.Parse dateString
printfn $"{offsetDate}"

// String with date and offset
let dateString = "05/01/2008 +1:00"
let offsetDate = DateTimeOffset.Parse dateString
printfn $"{offsetDate}"

// String with day abbreviation
let dateString = "Thu May 01, 2008"
let offsetDate = DateTimeOffset.Parse(dateString)
printfn $"{offsetDate}"
Dim dateString As String
Dim offsetDate As DateTimeOffset

' String with date only
dateString = "05/01/2008"
offsetDate = DateTimeOffset.Parse(dateString)
Console.WriteLine(offsetDate.ToString())   ' Displays 5/1/2008 12:00:00 AM -07:00  

' String with time only
dateString = "11:36 PM"
offsetDate = DateTimeOffset.Parse(dateString)
Console.WriteLine(offsetDate.ToString())   ' Displays 3/26/2007 11:36:00 PM -07:00

' String with date and offset 
dateString = "05/01/2008 +7:00"
offsetDate = DateTimeOffset.Parse(dateString)
Console.WriteLine(offsetDate.ToString())   ' Displays 5/1/2008 12:00:00 AM +07:00

' String with day abbreviation
dateString = "Thu May 01, 2008"
offsetDate = DateTimeOffset.Parse(dateString)
Console.WriteLine(offsetDate.ToString())   ' Displays 5/1/2008 12:00:00 AM -07:00

Remarks

Parse(String) parses a string with three elements that can appear in any order and are delimited by white space. These three elements are as shown in the following table.

Element Example
<Date> "2/10/2007"
<Time> "1:02:03 PM"
<Offset> "-7:30"

Although each of these elements is optional, <Offset> cannot appear by itself. It must be provided together with either <Date> or <Time>. If <Date> is missing, its default value is the current day. If <Time> is missing, its default value is 12:00:00 AM. If <Date> is present but its year component consists of only two digits, it is converted to a year in the current culture's current calendar based on the value of the Calendar.TwoDigitYearMax property. If <Offset> is missing, its default value is the offset of the local time zone. <Offset> can represent either a negative or a positive offset from Coordinated Universal Time (UTC). In either case, <Offset> must include a sign symbol.

The input string is parsed by using the formatting information in a DateTimeFormatInfo object that is initialized for the current culture. To parse a string that contains designated formatting that does not necessarily correspond to the formatting of the current culture, use the ParseExact method and provide a format specifier.

See also

Applies to

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Parses a span of characters into a value.

public:
 static DateTimeOffset Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<DateTimeOffset>::Parse;
public static DateTimeOffset Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> DateTimeOffset
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As DateTimeOffset

Parameters

s
ReadOnlySpan<Char>

The span of characters to parse.

provider
IFormatProvider

An object that provides culture-specific formatting information about s.

Returns

The result of parsing s.

Implements

Applies to

Parse(String, IFormatProvider)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Converts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified culture-specific format information.

public:
 static DateTimeOffset Parse(System::String ^ input, IFormatProvider ^ formatProvider);
public:
 static DateTimeOffset Parse(System::String ^ input, IFormatProvider ^ formatProvider) = IParsable<DateTimeOffset>::Parse;
public static DateTimeOffset Parse (string input, IFormatProvider formatProvider);
public static DateTimeOffset Parse (string input, IFormatProvider? formatProvider);
static member Parse : string * IFormatProvider -> DateTimeOffset
Public Shared Function Parse (input As String, formatProvider As IFormatProvider) As DateTimeOffset

Parameters

input
String

A string that contains a date and time to convert.

formatProvider
IFormatProvider

An object that provides culture-specific format information about input.

Returns

An object that is equivalent to the date and time that is contained in input, as specified by formatProvider.

Implements

Exceptions

The offset is greater than 14 hours or less than -14 hours.

input is null.

input does not contain a valid string representation of a date and time.

-or-

input contains the string representation of an offset value without a date or time.

Examples

The following example parses date and time strings that are formatted for the fr-fr culture and displays them using the local system's default en-us culture.

DateTimeFormatInfo fmt = new CultureInfo("fr-fr").DateTimeFormat;
string dateString;
DateTimeOffset offsetDate;

dateString = "03-12-07";
offsetDate = DateTimeOffset.Parse(dateString, fmt);
Console.WriteLine("{0} returns {1}",
                  dateString,
                  offsetDate.ToString());

dateString = "15/09/07 08:45:00 +1:00";
offsetDate = DateTimeOffset.Parse(dateString, fmt);
Console.WriteLine("{0} returns {1}",
                  dateString,
                  offsetDate.ToString());

dateString = "mar. 1 janvier 2008 1:00:00 +1:00";
offsetDate = DateTimeOffset.Parse(dateString, fmt);
Console.WriteLine("{0} returns {1}",
                  dateString,
                  offsetDate.ToString());
// The example displays the following output to the console:
//    03-12-07 returns 12/3/2007 12:00:00 AM -08:00
//    15/09/07 08:45:00 +1:00 returns 9/15/2007 8:45:00 AM +01:00
//    mar. 1 janvier 2008 1:00:00 +1:00 returns 1/1/2008 1:00:00 AM +01:00
let fmt = CultureInfo("fr-fr").DateTimeFormat
let dateString = "03-12-07"
let offsetDate = DateTimeOffset.Parse(dateString, fmt)
printfn $"{dateString} returns {offsetDate}"

let dateString = "15/09/07 08:45:00 +1:00"
let offsetDate = DateTimeOffset.Parse(dateString, fmt)
printfn $"{dateString} returns {offsetDate}"

let dateString = "mar. 1 janvier 2008 1:00:00 +1:00"
let offsetDate = DateTimeOffset.Parse(dateString, fmt)
printfn $"{dateString} returns {offsetDate}"

// The example displays the following output to the console:
//    03-12-07 returns 12/3/2007 12:00:00 AM -08:00
//    15/09/07 08:45:00 +1:00 returns 9/15/2007 8:45:00 AM +01:00
//    mar. 1 janvier 2008 1:00:00 +1:00 returns 1/1/2008 1:00:00 AM +01:00
Dim fmt As DateTimeFormatInfo = New CultureInfo("fr-fr").DateTimeFormat
Dim dateString As String
Dim offsetDate As DateTimeOffset

dateString = "03-12-07"
offsetDate = DateTimeOffset.Parse(dateString, fmt)
Console.WriteLine("{0} returns {1}", _
                  dateString, _
                  offsetDate.ToString())

dateString = "15/09/07 08:45:00 +1:00"
offsetDate = DateTimeOffset.Parse(dateString, fmt)
Console.WriteLine("{0} returns {1}", _
                  dateString, _
                  offsetDate.ToString())

dateString = "mar. 1 janvier 2008 1:00:00 +1:00" 
offsetDate = DateTimeOffset.Parse(dateString, fmt)
Console.WriteLine("{0} returns {1}", _
                  dateString, _
                  offsetDate.ToString())
' The example displays the following output to the console:
'    03-12-07 returns 12/3/2007 12:00:00 AM -08:00
'    15/09/07 08:45:00 +1:00 returns 9/15/2007 8:45:00 AM +01:00
'    mar. 1 janvier 2008 1:00:00 +1:00 returns 1/1/2008 1:00:00 AM +01:00

Remarks

This 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
<Date> "2/10/2007"
<Time> "1:02:03 PM"
<Offset> "-7:30"

Although each of these elements is optional, <Offset> cannot appear by itself. It must be provided together with either <Date> or <Time>. If <Date> is missing, its default value is the current day. If <Date> is present but its year component consists of only two digits, it is converted to a year in the provider parameter's current calendar based on the value of the Calendar.TwoDigitYearMax property. If <Time> is missing, its default value is 12:00:00 AM. If <Offset> is missing, its default value is the offset of the local time zone. <Offset> can represent either a negative or a positive offset from Coordinated Universal Time (UTC). In either case, <Offset> must include a sign symbol.

The format of these three elements is defined by the formatProvider parameter, which can be either of the following:

If formatprovider is null, the CultureInfo object that corresponds to the current culture is used.

The positive or negative sign used in <Offset> must be either + or -. It is not defined by the PositiveSign or NegativeSign properties of the NumberFormatInfo object of the formatProvider parameter.

See also

Applies to

Parse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Converts the specified span representation of a date and time to its DateTimeOffset equivalent using the specified culture-specific format information and formatting style.

public static DateTimeOffset Parse (ReadOnlySpan<char> input, IFormatProvider? formatProvider = default, System.Globalization.DateTimeStyles styles = System.Globalization.DateTimeStyles.None);
public static DateTimeOffset Parse (ReadOnlySpan<char> input, IFormatProvider formatProvider = default, System.Globalization.DateTimeStyles styles = System.Globalization.DateTimeStyles.None);
static member Parse : ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles -> DateTimeOffset
Public Shared Function Parse (input As ReadOnlySpan(Of Char), Optional formatProvider As IFormatProvider = Nothing, Optional styles As DateTimeStyles = System.Globalization.DateTimeStyles.None) As DateTimeOffset

Parameters

input
ReadOnlySpan<Char>

A span containing the characters that represent a date and time to convert.

formatProvider
IFormatProvider

An object that provides culture-specific format information about input.

styles
DateTimeStyles

A bitwise combination of enumeration values that indicates the permitted format of input. A typical value to specify is None.

Returns

An object that is equivalent to the date and time that is contained in input as specified by formatProvider and styles.

Applies to

Parse(String, IFormatProvider, DateTimeStyles)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Converts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified culture-specific format information and formatting style.

public:
 static DateTimeOffset Parse(System::String ^ input, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles);
public static DateTimeOffset Parse (string input, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles);
public static DateTimeOffset Parse (string input, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles);
static member Parse : string * IFormatProvider * System.Globalization.DateTimeStyles -> DateTimeOffset
Public Shared Function Parse (input As String, formatProvider As IFormatProvider, styles As DateTimeStyles) As DateTimeOffset

Parameters

input
String

A string that contains a date and time to convert.

formatProvider
IFormatProvider

An object that provides culture-specific format information about input.

styles
DateTimeStyles

A bitwise combination of enumeration values that indicates the permitted format of input. A typical value to specify is None.

Returns

An object that is equivalent to the date and time that is contained in input as specified by formatProvider and styles.

Exceptions

The offset is greater than 14 hours or less than -14 hours.

-or-

styles is not a valid DateTimeStyles value.

-or-

styles includes an unsupported DateTimeStyles value.

-or-

styles includes DateTimeStyles values that cannot be used together.

input is null.

input does not contain a valid string representation of a date and time.

-or-

input contains the string representation of an offset value without a date or time.

Examples

The following example illustrates the effect of passing the DateTimeStyles.AssumeLocal, DateTimeStyles.AssumeUniversal, and DateTimeStyles.AdjustToUniversal values to the styles parameter of the Parse(String, IFormatProvider, DateTimeStyles) method.

string dateString;
DateTimeOffset offsetDate;

dateString = "05/01/2008 6:00:00";
// Assume time is local
offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AssumeLocal);
Console.WriteLine(offsetDate.ToString());   // Displays 5/1/2008 6:00:00 AM -07:00

// Assume time is UTC
offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AssumeUniversal);
Console.WriteLine(offsetDate.ToString());   // Displays 5/1/2008 6:00:00 AM +00:00

// Parse and convert to UTC
dateString = "05/01/2008 6:00:00AM +5:00";
offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AdjustToUniversal);
Console.WriteLine(offsetDate.ToString());   // Displays 5/1/2008 1:00:00 AM +00:00
let dateString = "05/01/2008 6:00:00"
// Assume time is local
let offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AssumeLocal)
printfn $"{offsetDate}"   // Displays 5/1/2008 6:00:00 AM -07:00

// Assume time is UTC
let offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AssumeUniversal)
printfn $"{offsetDate}"   // Displays 5/1/2008 6:00:00 AM +00:00

// Parse and convert to UTC
let dateString = "05/01/2008 6:00:00AM +5:00"
let offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AdjustToUniversal)
printfn $"{offsetDate}"   // Displays 5/1/2008 1:00:00 AM +00:00
Dim dateString As String
Dim offsetDate As DateTimeOffset

dateString = "05/01/2008 6:00:00"
' Assume time is local 
offsetDate = DateTimeOffset.Parse(dateString, Nothing, DateTimeStyles.AssumeLocal)
Console.WriteLine(offsetDate.ToString())   ' Displays 5/1/2008 6:00:00 AM -07:00

' Assume time is UTC
offsetDate = DateTimeOffset.Parse(dateString, Nothing, DateTimeStyles.AssumeUniversal)
Console.WriteLine(offsetDate.ToString())   ' Displays 5/1/2008 6:00:00 AM +00:00

' Parse and convert to UTC 
dateString = "05/01/2008 6:00:00AM +5:00"
offsetDate = DateTimeOffset.Parse(dateString, Nothing, DateTimeStyles.AdjustToUniversal)
Console.WriteLine(offsetDate.ToString())   ' Displays 5/1/2008 1:00:00 AM +00:00

Remarks

This 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
<Date> "2/10/2007"
<Time> "1:02:03 PM"
<Offset> "-7:30"

Although each of these elements is optional, <Offset> cannot appear by itself. It must be provided together with either <Date> or <Time>. If <Date> is missing, its default value is the current day. If <Date> is present but its year component consists of only two digits, it is converted to a year in the provider parameter's current calendar based on the value of the Calendar.TwoDigitYearMax property. If <Time> is missing, its default value is 12:00:00 AM. If <Offset> is missing, its default value is the offset of the local time zone, or TimeSpan.Zero if either the DateTimeStyles.AdjustToUniversal or DateTimeStyles.AssumeUniversal value is specified in the styles parameter. If <Offset> is present, it can represent either a negative or a positive offset from Coordinated Universal Time (UTC). In either case, <Offset> must include a sign symbol.

The format of these three elements is defined by the formatProvider parameter, which can be either of the following:

Each element can also be enclosed by leading or trailing white space, and the <Date> and <Time> elements can include inner white space (such as 6: 00:00). Only the <Offset> component cannot include inner white space.

If formatprovider is null, the CultureInfo object that corresponds to the current culture is used.

The positive or negative sign used in <Offset> must be either + or -. It is not defined by the PositiveSign or NegativeSign properties of the NumberFormatInfo object returned by the formatProvider parameter.

The following table shows the members of the System.Globalization.DateTimeStyles enumeration that are supported.

DateTimeStyles member Description
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 DateTimeOffset.ToUniversalTime method of the returned DateTimeOffset object.
AllowInnerWhite Although valid, this value is ignored. Inner white space is permitted in the <Date> and <Time> elements.
AllowLeadingWhite Although valid, this value is ignored. Leading white space is permitted in front of each component in the parsed string.
AllowTrailingWhite Although valid, this value is ignored. Trailing white space is permitted 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 <Offset> element, the offset of the local time zone should be provided. This is the default behavior of the Parse method.
AssumeUniversal Indicates that, if the input parameter lacks an <Offset> 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.

See also

Applies to