DateTimeOffset.Parse Method (String)
Converts the specified string representation of a date, time, and offset to its DateTimeOffset equivalent.
Assembly: mscorlib (in mscorlib.dll)
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.
input contains the string representation of an offset value without a date or time.
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.
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.
The following example calls themethod 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());
Available since 8
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1