XmlConvert.ToDateTime 方法

定义

String 转换为等效的 DateTime

重载

ToDateTime(String)
已过时.
已过时.

String 转换为等效的 DateTime

ToDateTime(String, String)

String 转换为等效的 DateTime

ToDateTime(String, String[])

String 转换为等效的 DateTime

ToDateTime(String, XmlDateTimeSerializationMode)

使用指定的 XmlDateTimeSerializationModeString 转换为 DateTime

ToDateTime(String)

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

注意

Use XmlConvert.ToDateTime() that takes in XmlDateTimeSerializationMode

注意

Use XmlConvert.ToDateTime() that accepts an XmlDateTimeSerializationMode instead.

String 转换为等效的 DateTime

public:
 static DateTime ToDateTime(System::String ^ s);
[System.Obsolete("Use XmlConvert.ToDateTime() that takes in XmlDateTimeSerializationMode")]
public static DateTime ToDateTime (string s);
[System.Obsolete("Use XmlConvert.ToDateTime() that accepts an XmlDateTimeSerializationMode instead.")]
public static DateTime ToDateTime (string s);
public static DateTime ToDateTime (string s);
[<System.Obsolete("Use XmlConvert.ToDateTime() that takes in XmlDateTimeSerializationMode")>]
static member ToDateTime : string -> DateTime
[<System.Obsolete("Use XmlConvert.ToDateTime() that accepts an XmlDateTimeSerializationMode instead.")>]
static member ToDateTime : string -> DateTime
static member ToDateTime : string -> DateTime
Public Shared Function ToDateTime (s As String) As DateTime

参数

s
String

要转换的字符串。

返回

与该字符串等效的 DateTime

属性

例外

snull

s 为空字符串或格式不正确。

示例

以下示例使用 ToDoubleToDateTime 读取强类型数据。

#using <System.dll>
#using <System.xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = gcnew XmlTextReader( "orderData.xml" );
   
   //Parse the file and pull out the order date and price.
   while ( reader->Read() )
   {
      if ( reader->NodeType == XmlNodeType::Element )
      {
         if ( reader->Name->Equals( "order" ) )
         {
            DateTime orderDate = XmlConvert::ToDateTime( reader->GetAttribute( "date" ) );
            Console::WriteLine( "order date: {0}", orderDate.ToString() );
         }
         else
         if ( reader->Name->Equals( "price" ) )
         {
            Double price = XmlConvert::ToDouble( reader->ReadInnerXml() );
            Console::WriteLine( "price: {0}", price );
         }
      }
   }

   
   //Close the reader.
   reader->Close();
}
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {
    XmlTextReader reader = new XmlTextReader("orderData.xml");

    //Parse the file and pull out the order date and price.
    while (reader.Read()){
       if (reader.NodeType==XmlNodeType.Element){
         switch(reader.Name){
           case "order":
             DateTime orderDate = XmlConvert.ToDateTime(reader.GetAttribute("date"));
             Console.WriteLine("order date: {0}", orderDate.ToString());
             break;
           case "price":
             Double price = XmlConvert.ToDouble(reader.ReadInnerXml());
             Console.WriteLine("price: {0}", price.ToString());
             break;
         }
       }
    }

    //Close the reader.
    reader.Close();
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()
  
    Dim reader as XmlTextReader = new XmlTextReader("orderData.xml")

    'Parse the file and pull out the order date and price.
    while (reader.Read())
       if (reader.NodeType=XmlNodeType.Element)
         select case reader.Name
           case "order":
             Dim orderDate as DateTime = XmlConvert.ToDateTime(reader.GetAttribute("date"))
             Console.WriteLine("order date: {0}", orderDate.ToString())
           case "price":
             Dim price as Double = XmlConvert.ToDouble(reader.ReadInnerXml())
             Console.WriteLine("price: {0}", price.ToString())
         end select
       end if
    end while

    'Close the reader.
    reader.Close()  
  end sub
end class

该示例使用 文件 orderData.xml作为输入。

<order date="2001-05-03">
  <orderID>367A54</orderID>
  <custID>32632</custID>
  <price>19.95</price>
</order>

注解

注意

方法XmlConvert.ToDateTime(String)在 .NET Framework的 2.0 版本中已过时,已被 方法替换XmlConvert.ToDateTime(String, XmlDateTimeSerializationMode)

适用于

ToDateTime(String, String)

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

String 转换为等效的 DateTime

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

参数

s
String

要转换的字符串。

format
String

该格式结构适用于转换的 DateTime。 有效格式包括“yyyy-MM-ddTHH:mm:sszzzzzz”及其子集。 根据此格式验证字符串。

返回

与该字符串等效的 DateTime

例外

snull

sformat 为 String.Empty

- 或 -

s 不包含与 format 对应的日期和时间。

适用于

ToDateTime(String, String[])

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

String 转换为等效的 DateTime

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

参数

s
String

要转换的字符串。

formats
String[]

包含格式结构的数组,这些格式结构适用于转换的 DateTime。 有效格式包括“yyyy-MM-ddTHH:mm:sszzzzzz”及其子集。

返回

与该字符串等效的 DateTime

例外

snull

sformats 的一个元素为 String.Empty

- 或 -

s 不包含与 formats 的任何元素对应的日期和时间。

注解

此方法允许验证字符串的多种格式。

适用于

ToDateTime(String, XmlDateTimeSerializationMode)

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

使用指定的 XmlDateTimeSerializationModeString 转换为 DateTime

public:
 static DateTime ToDateTime(System::String ^ s, System::Xml::XmlDateTimeSerializationMode dateTimeOption);
public static DateTime ToDateTime (string s, System.Xml.XmlDateTimeSerializationMode dateTimeOption);
static member ToDateTime : string * System.Xml.XmlDateTimeSerializationMode -> DateTime
Public Shared Function ToDateTime (s As String, dateTimeOption As XmlDateTimeSerializationMode) As DateTime

参数

s
String

要转换的 String 值。

dateTimeOption
XmlDateTimeSerializationMode

枚举值之一,用于指定日期是应转换为本地时间,还是应保留为协调通用时间 (UTC)(如果它为 UTC 日期)。

返回

DateTime 的等效 String

例外

snull

dateTimeOptionnull

s 是一个空字符串或其格式无效。

适用于