Language

XmlConvert.ToDateTimeOffset Method

Definition

Converts the supplied String to a DateTimeOffset equivalent.

Overloads

Name Description
ToDateTimeOffset(String, String[])

Converts the supplied String to a DateTimeOffset equivalent.

ToDateTimeOffset(String, String)

Converts the supplied String to a DateTimeOffset equivalent.

ToDateTimeOffset(String)

Converts the supplied String to a DateTimeOffset equivalent.

ToDateTimeOffset(String, String[])

Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs

Converts the supplied String to a DateTimeOffset equivalent.

public:
 static DateTimeOffset ToDateTimeOffset(System::String ^ s, cli::array <System::String ^> ^ formats);
public static DateTimeOffset ToDateTimeOffset(string s, string[] formats);
static member ToDateTimeOffset : string * string[] -> DateTimeOffset
Public Shared Function ToDateTimeOffset (s As String, formats As String()) As DateTimeOffset

Parameters

s
String

The string to convert.

formats
String[]

An array of .NET standard or custom date and time format strings that specify the permitted formats of s.

Returns

The DateTimeOffset equivalent of the supplied string.

Exceptions

An element of formats is invalid, or s doesn't exactly match any element of formats under the invariant culture rules, except for leading or trailing white space.

Examples

The following example demonstrates how to read a string from an XML file and use the ToDateTimeOffset method to convert the string to a DateTimeOffset type. The input string must validate against one of the specified formats before being converted.

using System;
using System.Xml;

class Example
{
    static void Main()
    {
        // Create an XmlReader, read to the "time" element, and read contents as type string
        XmlReader reader = XmlReader.Create("transactions.xml");
        reader.ReadToFollowing("time");
        string time = reader.ReadElementContentAsString();

        // Specify formats against which time will be validated before conversion to DateTimeOffset
        // If time does not match one of the specified formats, a FormatException will be thrown.
        // Each specified format must be a subset of the W3C Recommendation for the XML dateTime type
        string[] formats = {"yyyy-MM-ddTHH:mm:sszzzzzzz", "yyyy-MM-ddTHH:mm:ss", "yyyy-MM-dd"};
        try
        {
            // Read the element contents as a string and covert to DateTimeOffset type
            DateTimeOffset transaction_time = XmlConvert.ToDateTimeOffset(time, formats);
            Console.WriteLine(transaction_time);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}
Imports System.Xml

Module Module1
    Sub Main()
        ' Create an XmlReader, read to the "time" element, and read contents as type string
        Dim reader As XmlReader = XmlReader.Create("transactions.xml")
        reader.ReadToFollowing("time")
        Dim time As String = reader.ReadElementContentAsString()

        ' Specify formats against which time will be validated before conversion to DateTimeOffset
        ' If time does not match one of the specified formats, a FormatException will be thrown.
        ' Each specified format must be a subset of the W3C Recommendation for the XML dateTime type
        Dim formats As String() = {"yyyy-MM-ddTHH:mm:sszzzzzzz", "yyyy-MM-ddTHH:mm:ss", "yyyy-MM-dd"}
        Try
            ' Read the element contents as a string and covert to DateTimeOffset type
            Dim transaction_time As DateTimeOffset = XmlConvert.ToDateTimeOffset(time, formats)
            Console.WriteLine(transaction_time)
        Catch e As Exception
            Console.WriteLine(e)
        End Try
    End Sub
End Module

The example uses the transactions.xml file.

<?xml version="1.0"?>
<transactions>
   <transaction>
      <id>123456789</id>
      <amount>1.00</amount>
      <currency>USD</currency>
      <time>2007-08-03T22:05:13-07:00</time>
   </transaction>
</transactions>

Remarks

This overload is equivalent to calling DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) with DateTimeFormatInfo.InvariantInfo and the AllowLeadingWhite and AllowTrailingWhite flags. Leading and trailing white space is ignored, but s must otherwise exactly match one of the strings in formats. The formats parameter uses .NET standard date and time format strings or custom date and time format strings, not XML Schema dateTime patterns.

In a custom format string, the z, zz, and zzz specifiers require a signed numeric UTC offset. They don't match the XML UTC designator Z. Use the K custom format specifier when s can contain Z.

If the offset specified within the input string will cause an overflow in the deserialized representation of the DateTimeOffset, a FormatException is thrown.

When more than seven digits are specified for fractional seconds, the value is rounded. For example, 00000004 becomes 0000000 and 00000005 becomes 0000001.

Applies to

ToDateTimeOffset(String, String)

Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs

Converts the supplied String to a DateTimeOffset equivalent.

public:
 static DateTimeOffset ToDateTimeOffset(System::String ^ s, System::String ^ format);
public static DateTimeOffset ToDateTimeOffset(string s, string format);
static member ToDateTimeOffset : string * string -> DateTimeOffset
Public Shared Function ToDateTimeOffset (s As String, format As String) As DateTimeOffset

Parameters

s
String

The string to convert.

format
String

A .NET standard or custom date and time format string that specifies the required format of s.

Returns

The DateTimeOffset equivalent of the supplied string.

Exceptions

format is invalid, or s doesn't exactly match format under the invariant culture rules, except for leading or trailing white space.

Examples

The following example demonstrates how to read a string from an XML file and use the ToDateTimeOffset method to convert the string to a DateTimeOffset type. The input string is validated against the specified format before being converted.

using System;
using System.Xml;

class Example
{
    static void Main()
    {
        // Create an XmlReader, read to the "time" element, and read contents as type string
        XmlReader reader = XmlReader.Create("transactions.xml");
        reader.ReadToFollowing("time");
        string time = reader.ReadElementContentAsString();

        // Specify a format against which time will be validated before conversion to DateTimeOffset
        // If time does not match the format, a FormatException will be thrown.
        // The specified format must be a subset of the W3C Recommendation for the XML dateTime type
        string format = "yyyy-MM-ddTHH:mm:sszzzzzzz";
        try
        {
            // Read the element contents as a string and covert to DateTimeOffset type
            DateTimeOffset transaction_time = XmlConvert.ToDateTimeOffset(time, format);
            Console.WriteLine(transaction_time);
        }
        catch(Exception e)
        {
            Console.WriteLine(e);
        }
    }
}
Imports System.Xml

Module Module1      
    Sub Main()
        ' Create an XmlReader, read to the "time" element, and read contents as type string
        Dim reader As XmlReader = XmlReader.Create("transactions.xml")
        reader.ReadToFollowing("time")
        Dim time As String = reader.ReadElementContentAsString()

        ' Specify a format against which time will be validated before conversion to DateTimeOffset
        ' If time does not match the format, a FormatException will be thrown.
        ' The specified format must be a subset of the W3C Recommendation for the XML dateTime type
        Dim format As String = "yyyy-MM-ddTHH:mm:sszzzzzzz"
        Try
            ' Read the element contents as a string and covert to DateTimeOffset type
            Dim transaction_time As DateTimeOffset = XmlConvert.ToDateTimeOffset(time, format)
            Console.WriteLine(transaction_time)
        Catch e As Exception
            Console.WriteLine(e)
        End Try
    End Sub
End Module

The example uses the transactions.xml file.

<?xml version="1.0"?>
<transactions>
   <transaction>
      <id>123456789</id>
      <amount>1.00</amount>
      <currency>USD</currency>
      <time>2007-08-03T22:05:13-07:00</time>
   </transaction>
</transactions>

Remarks

This overload is equivalent to calling DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) with DateTimeFormatInfo.InvariantInfo and the AllowLeadingWhite and AllowTrailingWhite flags. Leading and trailing whitespace is ignored, but s must otherwise exactly match format. The format parameter uses .NET standard date and time format strings or custom date and time format strings, not XML Schema dateTime patterns.

In a custom format string, the z, zz, and zzz specifiers require a signed numeric UTC offset. They don't match the XML UTC designator Z. Use the K custom format specifier when s can contain Z.

If the offset specified within the input string will cause an overflow in the deserialized representation of the DateTimeOffset, a FormatException is thrown.

When more than seven digits are specified for fractional seconds, the value is rounded. For example, 00000004 becomes 0000000 and 00000005 becomes 0000001.

Applies to

ToDateTimeOffset(String)

Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs
Source:
XmlConvert.cs

Converts the supplied String to a DateTimeOffset equivalent.

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

Parameters

s
String

The string to convert. The string must conform to a subset of the W3C Recommendation for the XML dateTime type. For more information, see the dateTime section of the XML Schema specification.

Returns

The DateTimeOffset equivalent of the supplied string.

Exceptions

The argument passed to this method is outside the range of allowable values. For information about allowable values, see DateTimeOffset.

The argument passed to this method does not conform to a subset of the W3C Recommendations for the XML dateTime type. For more information, see the dateTime section of the XML Schema specification.

Examples

The following example demonstrates how to read a string from an XML file and use the ToDateTimeOffset method to convert the string to a DateTimeOffset type.

using System;
using System.Xml;

class Example
{
    static void Main()
    {
        // Create an XmlReader, read to the "time" element, and read contents as type string
        XmlReader reader = XmlReader.Create("transactions.xml");
        reader.ReadToFollowing("time");
        string time = reader.ReadElementContentAsString();

        // Read the element contents as a string and covert to DateTimeOffset type
        // The format of time must be a subset of the W3C Recommendation for the XML dateTime type
        DateTimeOffset transaction_time = XmlConvert.ToDateTimeOffset(time);
        Console.WriteLine(transaction_time);
    }
}
Imports System.Xml

Module Module1
    Sub Main()
        ' Create an XmlReader, read to the "time" element, and read contents as type string
        Dim reader As XmlReader = XmlReader.Create("transactions.xml")
        reader.ReadToFollowing("time")
        Dim time As String = reader.ReadElementContentAsString()

        ' Read the element contents as a string and covert to DateTimeOffset type
    ' The format of time must be a subset of the W3C Recommendation for the XML dateTime type
        Dim transaction_time As DateTimeOffset = XmlConvert.ToDateTimeOffset(time)
        Console.WriteLine(transaction_time)
    End Sub
End Module

The example uses the transactions.xml file.

<?xml version="1.0"?>
<transactions>
   <transaction>
      <id>123456789</id>
      <amount>1.00</amount>
      <currency>USD</currency>
      <time>2007-08-03T22:05:13-07:00</time>
   </transaction>
</transactions>

Remarks

When more than seven digits are specified for fractional seconds, the value is rounded. For example, 00000004 becomes 0000000 and 00000005 becomes 0000001.

Applies to