String was not recognized as a valid datetime in vb.net?
Asked
Active
Viewed 1.4k times
-1
-
1Care to give us *anything* beyond (what appears to be) an error message? – Damien_The_Unbeliever Oct 17 '12 at 07:18
-
1Please post your code here.............. this is the problem when your giving the string to a date type. Or date time is not in acceptable format. – andy Oct 17 '12 at 07:18
-
Please post (1) the code (2) the problem string. Otherwise we are just guessing and it's very hard to help. – MarkJ Oct 17 '12 at 11:41
1 Answers
3
It may be because you are converting a date which is represented as a string and is not on the standard date format. Ex,
Dim _dateString As String = "22-2009-11"
Dim _date As DateTime = = DateTime.Parse(_dateString);
throws an exception because _dateString
is non-standard date format. The best thing you will do is to use DateTime.ParseExact
Dim _dateString As String = "22-2009-11"
Dim _date As DateTime = DateTime.ParseExact(_dateString,"dd-yyyy-MM",CultureInfo.InvariantCulture);
-
I would say "a *recognised* date format" rather than "a *standard* date format". [`DateTime.Parse`](http://msdn.microsoft.com/en-us/library/1k1skd40.aspx) always recognises some standard date-time formats like ISO 8601 and RFC 1123, but there are also formats that are dependent on the current thread's locale. A [classic way to get this error](http://msdn.microsoft.com/en-us/library/1k1skd40.aspx) is to wrongly assume that your own cultural format is an international standard. – MarkJ Oct 17 '12 at 11:41