-1

String was not recognized as a valid datetime in vb.net?

jww
  • 97,681
  • 90
  • 411
  • 885
user1740636
  • 19
  • 1
  • 1
  • 4
  • 1
    Care to give us *anything* beyond (what appears to be) an error message? – Damien_The_Unbeliever Oct 17 '12 at 07:18
  • 1
    Please 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 Answers1

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);
Vivek S.
  • 19,945
  • 7
  • 68
  • 85
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 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