DateTimeOffset.Parse Method (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.
Assembly: mscorlib (in mscorlib.dll)
public static DateTimeOffset Parse( string input, IFormatProvider formatProvider, DateTimeStyles styles )
- Type: System.String
A string that contains a date and time to convert.
- Type: System.IFormatProvider
An object that provides culture-specific format information about input.
Return ValueType: System.DateTimeOffset
An object that is equivalent to the date and time that is contained in input as specified by formatProvider and styles.
The offset is greater than 14 hours or less than -14 hours.
styles is not a valid DateTimeStyles value.
styles includes an unsupported DateTimeStyles value.
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.
input contains the string representation of an offset value without a date or time.
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.
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:
A CultureInfo object that represents the culture whose formatting is used in the input parameter. The DateTimeFormatInfo object returned by the CultureInfo.DateTimeFormat property defines the formatting used in input.
A DateTimeFormatInfo object that defines the format of date and time data.
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.
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.
Although valid, this value is ignored. Inner white space is permitted in the <Date> and <Time> elements.
Although valid, this value is ignored. Leading white space is permitted in front of each component in the parsed string.
Although valid, this value is ignored. Trailing white space is permitted in front of each component in the parsed string.
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.
Indicates that, if the input parameter lacks an <Offset> element, the UTC offset (00:00) should be provided.
Although valid, this value is ignored and has no effect.
Because the DateTimeOffset structure does not include a Kind property, this value has no effect.
The following example illustrates the effect of passing the DateTimeStyles.AssumeLocal, DateTimeStyles.AssumeUniversal, and DateTimeStyles.AdjustToUniversal values to the styles parameter of the 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
.NET FrameworkSupported in: 4.5.2, 4.5.1, 4.5, 4, 3.5 SP1, 3.0 SP1, 2.0 SP1
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.