Mono Class Library: System.Xml.XmlConvert Overview | Members

System.Xml.XmlConvert.ToDateTime Method

Converts a string to a DateTime equivalent. [Edit]

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

Parameters

s
The string to convert. [Edit]
formats
A string array specifying formats used to validate s. [Edit]
formats
Documentation for this section has not yet been entered. [Edit]

Returns

The DateTime equivalent of s. [Edit]

Exceptions

TypeReason
ArgumentNullExceptions is a null reference. [Edit]
FormatException

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.

[Edit]

Remarks

This method calls DateTime.ParseExact(string, string, IFormatProvider)(s, formats, System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.AllowLeadingWhite|System.Globalization.DateTimeStyles.AllowTrailingWhite).

This method allows s to be validated against multiple formats.

Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets.

[Edit]

Example

The following example converts a string to a DateTime and writes the result to the console.

C# Example
using System;
using System.Xml;

public class App {

  public static void Main() {

    String someDate = "1966-09-19T03:45:11Z";
    String[] datetimeFormats = new String[]
      {"HH:mm:ss", "yyyy-MM-ddTHH:mm:ssZ"};
    DateTime dateTime =
      XmlConvert.ToDateTime(someDate, datetimeFormats);
    Console.WriteLine( "{0}", dateTime.ToString() );
  }
}

The output is

9/18/1966 8:45:11 PM

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0