言語

XmlConvert.ToDateTimeOffset メソッド

定義

指定した String を同等の DateTimeOffset に変換します。

オーバーロード

名前 説明
ToDateTimeOffset(String, String[])

指定した String を同等の DateTimeOffset に変換します。

ToDateTimeOffset(String, String)

指定した String を同等の DateTimeOffset に変換します。

ToDateTimeOffset(String)

指定した String を同等の DateTimeOffset に変換します。

ToDateTimeOffset(String, String[])

ソース:
XmlConvert.cs
ソース:
XmlConvert.cs
ソース:
XmlConvert.cs
ソース:
XmlConvert.cs
ソース:
XmlConvert.cs

指定した String を同等の DateTimeOffset に変換します。

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

パラメーター

s
String

変換する文字列。

formats
String[]

sの許可.NET形式を指定する標準またはカスタムの日時書式指定文字列の配列。

返品

DateTimeOffset指定された文字列に相当します。

例外

formatsの要素が無効であるか、sがインバリアント カルチャ 規則の下のformatsの要素と完全に一致しません (先頭または末尾の空白を除く)。

次の例では、XML ファイルから文字列を読み取り、 ToDateTimeOffset メソッドを使用して文字列を DateTimeOffset 型に変換する方法を示します。 入力文字列は、変換する前に、指定された形式のいずれかに対して検証する必要があります。

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

この例では、transactions.xml ファイルを使用します。

<?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>

注釈

このオーバーロードは、DateTimeFormatInfo.InvariantInfoフラグとAllowLeadingWhiteフラグとAllowTrailingWhite フラグを使用してDateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles)を呼び出すことと同じです。 先頭と末尾の空白は無視されますが、 sformatsのいずれかの文字列と完全に一致する必要があります。 formats パラメーターは、XML スキーマの dateTime パターンではなく、標準の日時書式指定文字列またはカスタム日時書式指定文字列.NET使用します。

カスタム書式指定文字列では、 zzz、および zzz 指定子には、符号付き数値 UTC オフセットが必要です。 XML UTC 指定子の Zと一致しません。 Zを含めることができる場合は、Kカスタム書式指定子s使用します。

入力文字列内で指定されたオフセットによって、DateTimeOffset の逆シリアル化表現でオーバーフローが発生する場合は、FormatException がスローされます。

秒の小数部に 7 桁を超える数字が指定されている場合、値は丸められます。 たとえば、00000004は 0000000 になり、00000005は0000001になります。

適用対象

ToDateTimeOffset(String, String)

ソース:
XmlConvert.cs
ソース:
XmlConvert.cs
ソース:
XmlConvert.cs
ソース:
XmlConvert.cs
ソース:
XmlConvert.cs

指定した String を同等の DateTimeOffset に変換します。

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

パラメーター

s
String

変換する文字列。

format
String

sの必要な形式を指定する.NET標準またはカスタムの日時書式指定文字列。

返品

DateTimeOffset指定された文字列に相当します。

例外

snullです。

format が無効であるか、 s がインバリアント カルチャ ルールの下の format と完全に一致しません (先頭または末尾の空白を除く)。

次の例では、XML ファイルから文字列を読み取り、 ToDateTimeOffset メソッドを使用して文字列を DateTimeOffset 型に変換する方法を示します。 入力文字列は、変換される前に、指定した形式に対して検証されます。

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

この例では、transactions.xml ファイルを使用します。

<?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>

注釈

このオーバーロードは、DateTimeFormatInfo.InvariantInfoフラグとAllowLeadingWhiteフラグとAllowTrailingWhite フラグを使用してDateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles)を呼び出すことと同じです。 先頭と末尾の空白は無視されますが、 sformatと完全に一致する必要があります。 format パラメーターは、XML スキーマの dateTime パターンではなく、標準の日時書式指定文字列またはカスタム日時書式指定文字列.NET使用します。

カスタム書式指定文字列では、 zzz、および zzz 指定子には、符号付き数値 UTC オフセットが必要です。 XML UTC 指定子の Zと一致しません。 Zを含めることができる場合は、Kカスタム書式指定子s使用します。

入力文字列内で指定されたオフセットによって、DateTimeOffset の逆シリアル化表現でオーバーフローが発生する場合は、FormatException がスローされます。

秒の小数部に 7 桁を超える数字が指定されている場合、値は丸められます。 たとえば、00000004は 0000000 になり、00000005は0000001になります。

適用対象

ToDateTimeOffset(String)

ソース:
XmlConvert.cs
ソース:
XmlConvert.cs
ソース:
XmlConvert.cs
ソース:
XmlConvert.cs
ソース:
XmlConvert.cs

指定した String を同等の DateTimeOffset に変換します。

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

パラメーター

s
String

変換する文字列。 文字列は、XML dateTime 型の W3C Recommendation のサブセットに準拠している必要があります。 詳細については、XML スキーマ仕様の dateTime セクションを参照してください。

返品

DateTimeOffset指定された文字列に相当します。

例外

snullです。

このメソッドに渡される引数が、許容値の範囲外です。 許容値については、 DateTimeOffsetを参照してください。

このメソッドに渡される引数は、XML dateTime 型の W3C Recommendations のサブセットに準拠していません。 詳細については、XML スキーマ仕様の dateTime セクションを参照してください。

次の例では、XML ファイルから文字列を読み取り、 ToDateTimeOffset メソッドを使用して文字列を DateTimeOffset 型に変換する方法を示します。

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

この例では、transactions.xml ファイルを使用します。

<?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>

注釈

秒の小数部に 7 桁を超える数字が指定されている場合、値は丸められます。 たとえば、00000004は 0000000 になり、00000005は0000001になります。

適用対象