DateTimeOffset.Parse Method (String)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Converts the specified string representation of a date, time, and offset to its DateTimeOffset equivalent.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Shared Function Parse ( _
input As String _
) As DateTimeOffset
public static DateTimeOffset Parse(
string input
)
Parameters
- input
Type: System.String
A string that contains a date and time to convert.
Return Value
Type: System.DateTimeOffset
An object that is equivalent to the date and time that is contained in input.
Exceptions
Exception | Condition |
---|---|
ArgumentException | The offset is greater than 14 hours or less than -14 hours. |
ArgumentNullException | input is nulla null reference (Nothing in Visual Basic). |
FormatException | 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. |
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:15" |
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 <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. This means that the input parameter must contain the current culture's representation of a date and time. 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.
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.
Dim dateString As String
Dim offsetDate As DateTimeOffset
' String with date only
dateString = "05/01/2008"
offsetDate = DateTimeOffset.Parse(dateString)
outputBlock.Text &= offsetDate.ToString() & vbCrLf ' Displays 5/1/2008 12:00:00 AM -07:00
' String with time only
dateString = "11:36 PM"
offsetDate = DateTimeOffset.Parse(dateString)
outputBlock.Text &= offsetDate.ToString() & vbCrLf ' 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)
outputBlock.Text &= offsetDate.ToString() & vbCrLf ' Displays 5/1/2008 12:00:00 AM +07:00
' String with day abbreviation
dateString = "Thu May 01, 2008"
offsetDate = DateTimeOffset.Parse(dateString)
outputBlock.Text &= offsetDate.ToString() & vbCrLf ' Displays 5/1/2008 12:00:00 AM -07:00
string dateString;
DateTimeOffset offsetDate;
// String with date only
dateString = "05/01/2008";
offsetDate = DateTimeOffset.Parse(dateString);
outputBlock.Text += offsetDate.ToString() + "\n";
// String with time only
dateString = "11:36 PM";
offsetDate = DateTimeOffset.Parse(dateString);
outputBlock.Text += offsetDate.ToString() + "\n";
// String with date and offset
dateString = "05/01/2008 +1:00";
offsetDate = DateTimeOffset.Parse(dateString);
outputBlock.Text += offsetDate.ToString() + "\n";
// String with day abbreviation
dateString = "Thu May 01, 2008";
offsetDate = DateTimeOffset.Parse(dateString);
outputBlock.Text += offsetDate.ToString() + "\n";
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.