Compartir a través de


DateTimeOffset.Parse Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Parse ( _
    input As String, _
    formatProvider As IFormatProvider _
) As DateTimeOffset
public static DateTimeOffset Parse(
    string input,
    IFormatProvider formatProvider
)

Parameters

  • input
    Type: System.String
    A string that contains a date and time to convert.
  • formatProvider
    Type: System.IFormatProvider
    An object that provides culture-specific format information about input.

Return Value

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

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

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: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 format of these three elements is defined by the formatProvider parameter, which can be either of the following:

If formatprovider is nulla null reference (Nothing in Visual Basic), the CultureInfo object that corresponds to the current culture is used to determine the specific valid formats for date and time elements.

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.

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.

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)
outputBlock.Text &= String.Format("{0} returns {1}", _
                  dateString, _
                  offsetDate.ToString()) & vbCrLf

dateString = "15/09/07 08:45:00 +1:00"
offsetDate = DateTimeOffset.Parse(dateString, fmt)
outputBlock.Text &= String.Format("{0} returns {1}", _
                  dateString, _
                  offsetDate.ToString()) & vbCrLf

dateString = "mar. 1 janvier 2008 1:00:00 +1:00"
offsetDate = DateTimeOffset.Parse(dateString, fmt)
outputBlock.Text &= String.Format("{0} returns {1}", _
                  dateString, _
                  offsetDate.ToString()) & vbCrLf
' The example displays the following output:
'    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                              
DateTimeFormatInfo fmt = new CultureInfo("fr-fr").DateTimeFormat;
string dateString;
DateTimeOffset offsetDate;

dateString = "03-12-07";
offsetDate = DateTimeOffset.Parse(dateString, fmt);
outputBlock.Text += String.Format("{0} returns {1}",
                  dateString,
                  offsetDate.ToString()) + "\n";

dateString = "15/09/07 08:45:00 +1:00";
offsetDate = DateTimeOffset.Parse(dateString, fmt);
outputBlock.Text += String.Format("{0} returns {1}",
                  dateString,
                  offsetDate.ToString()) + "\n";

dateString = "mar. 1 janvier 2008 1:00:00 +1:00";
offsetDate = DateTimeOffset.Parse(dateString, fmt);
outputBlock.Text += String.Format("{0} returns {1}",
                  dateString,
                  offsetDate.ToString()) + "\n";
// The example displays the following output:
//    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                              

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.