Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Date string to DateTime Format vb.net

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

here is my method to validate Datetime.

Example Date string : "27/03/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 to validate this date string formate to datetime.

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

I'm using asp.net with vb.net.

like image 525
Amr Elnashar Avatar asked Dec 01 '10 18:12

Amr Elnashar


1 Answers

Have a look at using DateTime.TryParseExact Method or DateTime.ParseExact Method

like image 139
Adriaan Stander Avatar answered Sep 30 '22 10:09

Adriaan Stander