XmlConvert.ToDateTimeOffset Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Converts the supplied String to a DateTimeOffset equivalent.
Overloads
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
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 formats from which s
can be converted. Each format in formats
can be any subset of the W3C Recommendation for the XML dateTime type. (For more information, see the dateTime section of the XML Schema specification..) The string s
is validated against one of these formats.
Returns
The DateTimeOffset equivalent of the supplied string.
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
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
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
The format from which s
is converted. The format parameter can be any subset of the W3C Recommendation for the XML dateTime type. (For more information, see the dateTime section of the XML Schema specification..) The string s
is validated against this format.
Returns
The DateTimeOffset equivalent of the supplied string.
Exceptions
s
is null
.
s
or format
is an empty string or is not in the specified format.
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
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
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
s
is null
.
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.