XmlConvert.ToDateTime Método

Definición

Convierte el String en un DateTime equivalente.Converts the String to a DateTime equivalent.

Sobrecargas

ToDateTime(String)

Convierte el String en un DateTime equivalente.Converts the String to a DateTime equivalent.

ToDateTime(String, String)

Convierte el String en un DateTime equivalente.Converts the String to a DateTime equivalent.

ToDateTime(String, String[])

Convierte el String en un DateTime equivalente.Converts the String to a DateTime equivalent.

ToDateTime(String, XmlDateTimeSerializationMode)

Convierte String en DateTime mediante el XmlDateTimeSerializationMode especificado.Converts the String to a DateTime using the XmlDateTimeSerializationMode specified.

ToDateTime(String)

Advertencia

Esta API está ya obsoleta.

Convierte el String en un DateTime equivalente.Converts the String to a DateTime equivalent.

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

Parámetros

s
String

Cadena que se va a convertir.The string to convert.

Devoluciones

Equivalente DateTime de la cadena.A DateTime equivalent of the string.

Atributos

Excepciones

s es null.s is null.

s es una cadena vacía o no tiene el formato correcto.s is an empty string or is not in the correct format.

Ejemplos

En el ejemplo siguiente se usa ToDouble y ToDateTime para leer datos fuertemente tipados.The following example uses ToDouble and ToDateTime to read strongly typed data.

#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

En el ejemplo se usa el archivo, orderData.xml, como entrada.The example uses the file, orderData.xml, as input.

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

Comentarios

Nota

El método XmlConvert.ToDateTime(String) está obsoleto en la versión 2,0 del .NET Framework y se ha reemplazado por el método XmlConvert.ToDateTime(String, XmlDateTimeSerializationMode).The XmlConvert.ToDateTime(String) method is obsolete in the 2.0 version of the .NET Framework and has been replaced by the XmlConvert.ToDateTime(String, XmlDateTimeSerializationMode) method.

ToDateTime(String, String)

Convierte el String en un DateTime equivalente.Converts the String to a DateTime equivalent.

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

Parámetros

s
String

Cadena que se va a convertir.The string to convert.

format
String

Estructura de formato que se va a aplicar al DateTime convertido.The format structure to apply to the converted DateTime. Entre los formatos válidos se incluyen "yyyy-MM-ddTHH:mm:sszzzzzz" y sus subconjuntos.Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets. La cadena se valida comparándola con este formato.The string is validated against this format.

Devoluciones

Equivalente DateTime de la cadena.A DateTime equivalent of the string.

Excepciones

s es null.s is null.

s o format es String.Emptys or format is String.Empty o bien-or- s no contiene ninguna fecha ni hora que se correspondan con format.s does not contain a date and time that corresponds to format.

ToDateTime(String, String[])

Convierte el String en un DateTime equivalente.Converts the String to a DateTime equivalent.

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

Parámetros

s
String

Cadena que se va a convertir.The string to convert.

formats
String[]

Matriz que contiene las estructuras de formato que se van a aplicar al DateTime convertido.An array containing the format structures to apply to the converted DateTime. Entre los formatos válidos se incluyen "yyyy-MM-ddTHH:mm:sszzzzzz" y sus subconjuntos.Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets.

Devoluciones

Equivalente DateTime de la cadena.A DateTime equivalent of the string.

Excepciones

s es null.s is null.

s o un elemento de formats es String.Emptys or an element of formats is String.Empty o bien-or- s no contiene una fecha y hora que se correspondan con ninguno de los elementos de formats.s does not contain a date and time that corresponds to any of the elements of formats.

Comentarios

Este método permite la validación de varios formatos para la cadena.This method allows multiple formats for the string to be validated against.

ToDateTime(String, XmlDateTimeSerializationMode)

Convierte String en DateTime mediante el XmlDateTimeSerializationMode especificado.Converts the String to a DateTime using the XmlDateTimeSerializationMode specified.

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

Parámetros

s
String

Valor String que se va a convertir.The String value to convert.

dateTimeOption
XmlDateTimeSerializationMode

Uno de los valores de la enumeración que especifican si la fecha debe convertirse a la hora local o mantenerse como hora universal coordinada (UTC), en el caso de que se trate de una fecha de UTC.One of the enumeration values that specify whether the date should be converted to local time or preserved as Coordinated Universal Time (UTC), if it is a UTC date.

Devoluciones

DateTime equivalente de la String.A DateTime equivalent of the String.

Excepciones

s es null.s is null.

dateTimeOption es null.dateTimeOption is null.

s es una cadena vacía o su formato no es válido.s is an empty string or is not in a valid format.

Se aplica a