Partager via


Convert.ToDateTime Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Shared Function ToDateTime ( _
    value As String _
) As DateTime
public static DateTime ToDateTime(
    string value
)

Parameters

  • 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.
-or-
A DateTime equivalent to DateTime.MinValue if value is nulla null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
FormatException

value is not a properly formatted date and time string.

Remarks

If value is not nulla null reference (Nothing in Visual Basic), 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 nulla null reference (Nothing in Visual Basic), 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.

Examples

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

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim dateString As String = Nothing

      ' Convert a null string.
      ConvertToDateTime(outputBlock, dateString)

      ' Convert an empty string.
      dateString = String.Empty
      ConvertToDateTime(outputBlock, dateString)

      ' Convert a non-date string.
      dateString = "not a date"
      ConvertToDateTime(outputBlock, dateString)

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

   Private Sub ConvertToDateTime(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal value As String)
      Dim convertedDate As Date
      Try
         convertedDate = Convert.ToDateTime(value)
         outputBlock.Text += String.Format("'{0}' converts to {1}.", value, convertedDate) & vbCrLf
      Catch e As FormatException
         outputBlock.Text += String.Format("'{0}' is not in the proper format.", value) & vbCrLf
      End Try
   End Sub
End Module
' The example displays the following output:
'    '' converts to 1/1/0001 12:00:00 AM.
'    '' 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.
'    'Tue Apr 28, 2009' converts to 4/28/2009 12:00:00 AM.
'    '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.
'    '17:32:47.003' converts to 5/30/2008 5:32:47 PM.
'    'Sat, 10 May 2008 14:32:17 GMT' converts to 5/10/2008 7:32:17 AM.
'    '2009-05-01T07:54:59.9843750-04:00' converts to 5/1/2009 4:54:59 AM.
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.
      ConvertToDateTime(dateString);

      // Convert an empty string.
      dateString = String.Empty;
      ConvertToDateTime(dateString);

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

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

   private static void ConvertToDateTime(string value)
   {
      DateTime convertedDate;
      try
      {
         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.

Version Information

Silverlight

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

Platforms

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