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)

'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
A DateTime equivalent to the value of value.
-or-
A DateTime equivalent to 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 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 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.


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.


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

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

Community Additions

ADD
Show:
© 2014 Microsoft