XmlConvert.ToDateTimeOffset 메서드

정의

제공된 String을 해당 DateTimeOffset으로 변환합니다.

오버로드

ToDateTimeOffset(String, String[])

제공된 String을 해당 DateTimeOffset으로 변환합니다.

ToDateTimeOffset(String, String)

제공된 String을 해당 DateTimeOffset으로 변환합니다.

ToDateTimeOffset(String)

제공된 String을 해당 DateTimeOffset으로 변환합니다.

ToDateTimeOffset(String, String[])

제공된 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를 변환할 수 있는 형식의 배열입니다. formats의 각 형식은 W3C 권장 사항 중 XML dateTime 형식에 대한 부분이 될 수 있습니다. (자세한 내용은 XML 스키마 사양의 dateTime 섹션을 참조하세요.) 이러한 형식 중 하나를 사용하여 문자열 s의 유효성을 검사합니다.

반환

DateTimeOffset

제공된 문자열에 해당하는 DateTimeOffset입니다.

예제

다음 예제에서는 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>  

설명

입력 문자열 내에 지정된 오프셋으로 인해 DateTimeOffset의 역직렬화된 표현에 오버플로가 발생하면 FormatException이 throw됩니다.

소수 자릿수 초 동안 7자리 이상의 숫자를 지정하면 값이 반올림됩니다. 예를 들어 00000004 00000000이 되고 00000005 0000001 됩니다.

적용 대상

ToDateTimeOffset(String, String)

제공된 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를 변환할 형식입니다. 형식 매개 변수는 W3C 권장 사항 중 XML dateTime 형식에 대한 부분이 될 수 있습니다. (자세한 내용은 XML 스키마 사양의 dateTime 섹션을 참조하세요.) 이 형식을 사용하여 문자열 s의 유효성을 검사합니다.

반환

DateTimeOffset

제공된 문자열에 해당하는 DateTimeOffset입니다.

예외

s이(가) null인 경우

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>  

설명

입력 문자열 내에 지정된 오프셋으로 인해 DateTimeOffset의 역직렬화된 표현에 오버플로가 발생하면 FormatException이 throw됩니다.

소수 자릿수 초 동안 7자리 이상의 숫자를 지정하면 값이 반올림됩니다. 예를 들어 00000004 00000000이 되고 00000005 0000001 됩니다.

적용 대상

ToDateTimeOffset(String)

제공된 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 권장 사항의 하위 집합을 준수해야 합니다. 자세한 내용은 XML 스키마 사양의 dateTime 섹션을 참조하세요.

반환

DateTimeOffset

제공된 문자열에 해당하는 DateTimeOffset입니다.

예외

s이(가) null인 경우

이 메서드에 전달된 인수가 허용되는 값 범위를 벗어나는 경우. 허용되는 값에 대한 자세한 내용은 DateTimeOffset을 참조하십시오.

이 메서드에 전달된 인수가 W3C 권장 사항 중 XML dateTime 형식에 대한 부분을 준수하지 않는 경우. 자세한 내용은 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 00000000이 되고 00000005 0000001 됩니다.

적용 대상