XmlConvert.ToDateTimeOffset Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengonversi yang disediakan String menjadi DateTimeOffset setara.
Overload
| Nama | Deskripsi |
|---|---|
| ToDateTimeOffset(String, String[]) |
Mengonversi yang disediakan String menjadi DateTimeOffset setara. |
| ToDateTimeOffset(String, String) |
Mengonversi yang disediakan String menjadi DateTimeOffset setara. |
| ToDateTimeOffset(String) |
Mengonversi yang disediakan String menjadi DateTimeOffset setara. |
ToDateTimeOffset(String, String[])
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
Mengonversi yang disediakan String menjadi DateTimeOffset setara.
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
Parameter
- s
- String
String yang akan dikonversi.
- formats
- String[]
Array string format tanggal dan waktu standar atau kustom .NET yang menentukan format yang diizinkan dari s.
Mengembalikan
Setara DateTimeOffset dengan string yang disediakan.
Pengecualian
Elemen formats tidak valid, atau s tidak sama persis dengan elemen formats apa pun di bawah aturan budaya invarian, kecuali untuk spasi putih di depan atau di belakang.
Contoh
Contoh berikut menunjukkan cara membaca string dari file XML dan menggunakan ToDateTimeOffset metode untuk mengonversi string menjadi DateTimeOffset jenis. String input harus memvalidasi terhadap salah satu format yang ditentukan sebelum dikonversi.
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
Contohnya menggunakan file 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>
Keterangan
Kelebihan beban ini setara dengan panggilan DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) dengan DateTimeFormatInfo.InvariantInfo dan AllowLeadingWhite bendera dan AllowTrailingWhite . Spasi putih di depan dan berikutnya diabaikan, tetapi s harus sama persis dengan salah satu string di formats. Parameter formats menggunakan .NET string format tanggal dan waktu standar atau string format tanggal dan waktu kustom, bukan pola dateTime Skema XML.
Dalam string format kustom, penentu z, zz, dan zzz memerlukan offset UTC numerik yang ditandatangani. Mereka tidak cocok dengan pendesain ZXML UTC . Gunakan penentu K format kustom saat s dapat berisi Z.
Jika offset yang ditentukan dalam string input akan menyebabkan luapan dalam representasi deserialisasi DateTimeOffset, FormatException akan dilemparkan.
Ketika lebih dari tujuh digit ditentukan untuk detik pecahan, nilai dibulatkan. Misalnya, 00000004 menjadi 0000000 dan 00000005 menjadi 0000001.
Berlaku untuk
ToDateTimeOffset(String, String)
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
Mengonversi yang disediakan String menjadi DateTimeOffset setara.
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
Parameter
- s
- String
String yang akan dikonversi.
- format
- String
String format tanggal dan waktu standar atau kustom .NET yang menentukan format yang diperlukan dari s.
Mengembalikan
Setara DateTimeOffset dengan string yang disediakan.
Pengecualian
s adalah null.
format tidak valid, atau s tidak persis cocok format di bawah aturan budaya invariant, kecuali untuk spasi putih di depan atau di belakang.
Contoh
Contoh berikut menunjukkan cara membaca string dari file XML dan menggunakan ToDateTimeOffset metode untuk mengonversi string menjadi DateTimeOffset jenis. String input divalidasi terhadap format yang ditentukan sebelum dikonversi.
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
Contohnya menggunakan file 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>
Keterangan
Kelebihan beban ini setara dengan panggilan DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) dengan DateTimeFormatInfo.InvariantInfo dan AllowLeadingWhite bendera dan AllowTrailingWhite . Spasi kosong di depan dan di belakang diabaikan, tetapi s harus sama persis dengan format. Parameter format menggunakan .NET string format tanggal dan waktu standar atau string format tanggal dan waktu kustom, bukan pola dateTime Skema XML.
Dalam string format kustom, penentu z, zz, dan zzz memerlukan offset UTC numerik yang ditandatangani. Mereka tidak cocok dengan pendesain ZXML UTC . Gunakan penentu K format kustom saat s dapat berisi Z.
Jika offset yang ditentukan dalam string input akan menyebabkan luapan dalam representasi deserialisasi DateTimeOffset, FormatException akan dilemparkan.
Ketika lebih dari tujuh digit ditentukan untuk detik pecahan, nilai dibulatkan. Misalnya, 00000004 menjadi 0000000 dan 00000005 menjadi 0000001.
Berlaku untuk
ToDateTimeOffset(String)
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
- Sumber:
- XmlConvert.cs
Mengonversi yang disediakan String menjadi DateTimeOffset setara.
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
Parameter
- s
- String
String yang akan dikonversi. String harus sesuai dengan subset Rekomendasi W3C untuk jenis dateTime XML. Untuk informasi selengkapnya, lihat bagian dateTime dari spesifikasi Skema XML.
Mengembalikan
Setara DateTimeOffset dengan string yang disediakan.
Pengecualian
s adalah null.
Argumen yang diteruskan ke metode ini berada di luar rentang nilai yang diizinkan. Untuk informasi tentang nilai yang diizinkan, lihat DateTimeOffset.
Argumen yang diteruskan ke metode ini tidak sesuai dengan subset Rekomendasi W3C untuk jenis dateTime XML. Untuk informasi selengkapnya, lihat bagian dateTime dari spesifikasi Skema XML.
Contoh
Contoh berikut menunjukkan cara membaca string dari file XML dan menggunakan ToDateTimeOffset metode untuk mengonversi string menjadi DateTimeOffset jenis.
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
Contohnya menggunakan file 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>
Keterangan
Ketika lebih dari tujuh digit ditentukan untuk detik pecahan, nilai dibulatkan. Misalnya, 00000004 menjadi 0000000 dan 00000005 menjadi 0000001.