Convert date to DateTime vb.net format

I have this example and this gives me an exception. "Conversion from string x to dateTime is invalid"

here is my method of checking date and time.

Example Date String: "03/27/1985"

Public Function validateDateColumn(ByRef FieldName As String) As Boolean Try If IsDate(FieldName) Then Dim actualDate As DateTime = CDate(FieldName) Dim DtLicExp As DateTime = CDate(actualDate.ToString("d", Thread.CurrentThread.CurrentCulture)) FieldName = DtLicExp.ToString("MM/dd/yyyy") Return True End If Catch ex As Exception 'FieldName &= "Format must be MM/dd/yyyy" Return False End Try End Function 

any idea for checking date string form for datetime.

I want to convert this date "27/03/1985" to datetime.

I am using asp.net with vb.net.

+4
source share
3 answers

This implementation will parse dd/MM/yyyy format dates and update the date string to MM/dd/yyyy as needed. DateTime.TryParseExact allows you to specify the format of the date you want to parse.

 Public Function validateDateColumn(ByRef FieldName As String) As Boolean validateDateColumn = False Dim dateValue As DateTime if DateTime.TryParseExact(FieldName, _ "dd/MM/yyyy", CultureInfo.InvariantCulture, _ DateTimeStyles.None, dateValue) Then validateDateColumn = True FieldName = dateValue.ToString("MM/dd/yyyy") End If End Function 
+1
source

You can try the TryParse method.

 Dim myDateString as String = "7/7/2010" Dim myDate as DateTime Dim isDate As Boolean = DateTime.TryParse(myDateString, myDate) If isDate Then ' Yay I'm a real date End If 
0
source

All Articles