Export (0) Print
Expand All

Convert.ToDateTime Method (String)

Converts the specified string representation of a date and time to an equivalent date and time value.

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

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

Parameters

value
Type: System.String

The string representation of a date and time.

Return Value

Type: System.DateTime
The date and time equivalent of the value of value, or the date and time equivalent of DateTime.MinValue if value is Nothing.

ExceptionCondition
FormatException

value is not a properly formatted date and time string.

If value is not Nothing, the return value is the result of invoking the DateTime.Parse method on value using the formatting information in a DateTimeFormatInfo object that is 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 Nothing, 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 characters in value are 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.

Module ConversionToDateTime
   Public Sub Main()
      Dim dateString As String = Nothing 

      ' 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)
   End Sub 

   Private Sub ConvertToDateTime(value As String)
      Dim convertedDate As Date 
      Try
         convertedDate = Convert.ToDateTime(value)
         Console.WriteLine("'{0}' converts to {1}.", value, convertedDate)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in the proper format.", value)
      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.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft