XmlConvert.ToDateTime Method

Definition

Converts the String to a DateTime equivalent.

Overloads

ToDateTime(String)
Obsolete.
Obsolete.

Converts the String to a DateTime equivalent.

ToDateTime(String, String)

Converts the String to a DateTime equivalent.

ToDateTime(String, String[])

Converts the String to a DateTime equivalent.

ToDateTime(String, XmlDateTimeSerializationMode)

Converts the String to a DateTime using the XmlDateTimeSerializationMode specified.

ToDateTime(String)

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

Caution

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

Caution

Use XmlConvert.ToDateTime() that takes in XmlDateTimeSerializationMode

Converts the String to a DateTime equivalent.

C#
[System.Obsolete("Use XmlConvert.ToDateTime() that accepts an XmlDateTimeSerializationMode instead.")]
public static DateTime ToDateTime(string s);
C#
[System.Obsolete("Use XmlConvert.ToDateTime() that takes in XmlDateTimeSerializationMode")]
public static DateTime ToDateTime(string s);
C#
public static DateTime ToDateTime(string s);

Parameters

s
String

The string to convert.

Returns

A DateTime equivalent of the string.

Attributes

Exceptions

s is an empty string or is not in the correct format.

Examples

The following example uses ToDouble and ToDateTime to read strongly typed data.

C#
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();
  }
}

The example uses the file, orderData.xml, as input.

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

Remarks

Note

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.

Applies to

.NET 10 and other versions
Product Versions (Obsolete)
.NET (Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10)
.NET Framework 1.1 (2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1)
.NET Standard (2.0, 2.1)

ToDateTime(String, String)

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

Converts the String to a DateTime equivalent.

C#
public static DateTime ToDateTime(string s, string format);

Parameters

s
String

The string to convert.

format
String

The format structure to apply to the converted DateTime. Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets. The string is validated against this format.

Returns

A DateTime equivalent of the string.

Exceptions

s or format is String.Empty

-or-

s does not contain a date and time that corresponds to format.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(String, String[])

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

Converts the String to a DateTime equivalent.

C#
public static DateTime ToDateTime(string s, string[] formats);

Parameters

s
String

The string to convert.

formats
String[]

An array containing the format structures to apply to the converted DateTime. Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets.

Returns

A DateTime equivalent of the string.

Exceptions

s or an element of formats is String.Empty

-or-

s does not contain a date and time that corresponds to any of the elements of formats.

Remarks

This method allows multiple formats for the string to be validated against.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(String, XmlDateTimeSerializationMode)

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

Converts the String to a DateTime using the XmlDateTimeSerializationMode specified.

C#
public static DateTime ToDateTime(string s, System.Xml.XmlDateTimeSerializationMode dateTimeOption);

Parameters

s
String

The String value to convert.

dateTimeOption
XmlDateTimeSerializationMode

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.

Returns

A DateTime equivalent of the String.

Exceptions

dateTimeOption is null.

s is an empty string or is not in a valid format.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0