Tom Gullen
Tom Gullen

Reputation: 61719

C# String to DateTime

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

Upvotes: 1

Views: 966

Answers (5)

Paweł Dyda
Paweł Dyda

Reputation: 18662

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.

Upvotes: 0

Giorgi
Giorgi

Reputation: 30873

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

Upvotes: 0

naivists
naivists

Reputation: 33491

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); 

Upvotes: 4

Itay Karo
Itay Karo

Reputation: 18286

look for DateTime.ParseExact method.

Upvotes: 3

Steven Spielberg
Steven Spielberg

Reputation:

val = dateStart.Text.ToString("yyyy-M-d HH:mm:ss");

Upvotes: 0

Related Questions