Why does the following code, throw an exception: “Conversion from string “00-25-2011″ to type ‘Date’ is not valid.”?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Test As Date
Test = "2012-01-25"
MsgBox(Test)
Test = Format(DateAdd("d", -31, Test), "mm-dd-yyyy")
MsgBox(Test)
End Sub
The same code runs successfully in a VB6 app. I do not understand why the month part is: 00.
Use “MM” for months, not “mm” which denotes minutes.
Also, I recommend using DateTime.ParseExact as it gives you more control.