sforsandeep sforsandeep - 4 months ago 18
Vb.net Question

Date Parse triggers error in ASP.NET

I'm trying to parse a date string in 'mm/dd/yyyy' to date type. But it triggers an error saying


String was not recognized as a valid DateTime.


This is the code Im using

Dim mydate As Date
If filter = 4 Then
mydate = Date.ParseExact(datepart, "mm/dd/yy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
End If


I didnt understand what I did wrong.

This is the datepart value when the error tirggered

07/27/2016

Answer

Your date format is wrong, use MM/dd/yyyy instead mm/dd/yy try below

Date.ParseExact(datepart, "MM/dd/yyyy", CultureInfo.InvariantCulture)