Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Convert.ToDateTime Method (String)

Converts the specified String representation of a date and time to an equivalent DateTime.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static DateTime ToDateTime(
	string value


Type: System.String
The string representation of a date and time.

Return Value

Type: System.DateTime
A DateTime equivalent to the value of value.
A DateTime equivalent to DateTime.MinValue if value is null.


value is not a properly formatted date and time string.

If value is not null, the return value is the result of invoking the DateTime.Parse method on value using the formatting information in a DateTimeFormatInfo object initialized for the current culture. The value argument must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic. If value is null, the method returns DateTime.MinValue.

This method tries to parse value completely and avoid throwing a FormatException. It completes missing month, day, and year information with the current date. If value contains only a date and no time, this method assumes a time of midnight. Any leading, inner, or trailing white space character in value is ignored.

If you prefer not to handle an exception if the conversion fails, you can call the DateTime.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

The following example uses the ToDateTime method to convert various string representations of dates and times to DateTime values.

using System;

public class Example
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      Example.outputBlock = outputBlock;

      string dateString = null;

      // Convert a null string.

      // Convert an empty string.
      dateString = String.Empty;

      // Convert a non-date string.
      dateString = "not a date";

      // Try to convert various date strings.
      dateString = "05/01/1996";
      dateString = "Tue Apr 28, 2009";
      dateString = "Wed Apr 28, 2009";
      dateString = "06 July 2008 7:32:47 AM";
      dateString = "17:32:47.003";
      // Convert a string returned by DateTime.ToString("R").
      dateString = "Sat, 10 May 2008 14:32:17 GMT";
      // Convert a string returned by DateTime.ToString("o").
      dateString = "2009-05-01T07:54:59.9843750-04:00";

   private static void ConvertToDateTime(string value)
      DateTime convertedDate;
         convertedDate = Convert.ToDateTime(value);
         outputBlock.Text += String.Format("'{0}' converts to {1} {2} time.",
                           value, convertedDate,
                           convertedDate.Kind.ToString()) + "\n";
      catch (FormatException)
         outputBlock.Text += String.Format("'{0}' is not in the proper format.", value) + "\n";
// The example displays the following output:
//    '' converts to 1/1/0001 12:00:00 AM Unspecified time.
//    '' is not in the proper format.
//    'not a date' is not in the proper format.
//    '05/01/1996' converts to 5/1/1996 12:00:00 AM Unspecified time.
//    'Tue Apr 28, 2009' converts to 4/28/2009 12:00:00 AM Unspecified time.
//    'Wed Apr 28, 2009' is not in the proper format.
//    '06 July 2008 7:32:47 AM' converts to 7/6/2008 7:32:47 AM Unspecified time.
//    '17:32:47.003' converts to 5/30/2008 5:32:47 PM Unspecified time.
//    'Sat, 10 May 2008 14:32:17 GMT' converts to 5/10/2008 7:32:17 AM Local time.
//    '2009-05-01T07:54:59.9843750-04:00' converts to 5/1/2009 4:54:59 AM Local time.


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

© 2015 Microsoft