views:

85

answers:

5
DateTime frm_datestart = DateTime.Parse(dateStart.Text);

This line throws the error:

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Where the entered string is from Jquery-UI, examples:

 09/29/2010
 09/30/2010

Anyone know what the correct format should be? I'm suprised this isn't working :S

A: 
val = dateStart.Text.ToString("yyyy-M-d HH:mm:ss");
steven spielberg
That will probably not even compile (there is no `ToString` override for `String` that takes a `String` as parameter).
Fredrik Mörk
+3  A: 

look for DateTime.ParseExact method.

Itay
+4  A: 

You can use an overloaded version of the DateTime.Parse() method which accepts a second DateTimeFormatInfo parameter.

System.Globalization.DateTimeFormatInfo dti = new System.Globalization.DateTimeFormatInfo();
dti.ShortDatePattern = "MM/dd/yyyy";
DateTime dt = DateTime.Parse(dateStart.Text, dti); 
naivists
Perfect, thanks!
Tom Gullen
A: 

Use DateTime.ParseExact to specify format like this: DateTime.Parse("dd/MM/yyyy", dateStart.Text, null)

Giorgi
A: 

The problem with DateTime.ParseExact() method suggested in previous answers is, it fails on some Cultures. So your application may fail to run correctly on certain Operating Systems.

If you are sure that dateStart.Text will always be in the same format (i.e. en-US), you may try passing appropriate CultureInfo as a second argument. For format "MM/dd/yyyy" use CultureInfo.InvariantCulture.

Paweł Dyda