Mega Code Archive

 
Categories / VB.Net / Development
 

Convert ToDateTime (Object) converts object to a DateTime object

Module ConversionToDateTime    Public Sub Main()       Dim dateString As String        dateString = "05/01/2010"       ConvertToDateTime(dateString)       dateString = "Tue Apr 28, 2010"       ConvertToDateTime(dateString)       dateString = "06 July 2010 7:32:47 AM"       ConvertToDateTime(dateString)       dateString = "17:32:47.003"       ConvertToDateTime(dateString)    End Sub    Private Sub ConvertToDateTime(value As Object)       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)       Catch e As InvalidCastException          Console.WriteLine("Conversion of the {0} '{1}' is not supported", _                            value.GetType().Name, value)       End Try    End Sub End Module