Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
How to convert date from textbox.

If i run my webpage in local system it converting string to date but its not working if i run in server.

C#
DateTime date = DateTime.Parse(txt_date.Text);


error message : String was not recognized as a valid DateTime
Posted
Updated 27-Feb-14 8:38am
v2
Comments
Maciej Los 27-Feb-14 14:40pm    
ASP.NET? Please, improve your question and post page code and code behind.
Sergey Alexandrovich Kryukov 27-Feb-14 15:10pm    
Add the tag: "ASP.NET".
It depends on the input string, which can be in anything, it even can be time, but in different format. This approach makes very little sense.
—SA

It's going to depend on what the user enters into the textbox: to be honest, he could put anything!
This is why I don't use textboxes: I would suggest a Calendar[^] control instead: it will display in the local user format, but always give you the SelectedDate[^] property, which is a valid DateTime and needs no further conversion.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Feb-14 15:08pm    
Totally agree, a 5. There are some similar options, please see Solution 2.
—SA
OriginalGriff is perfectly right. There are more similar options; please see my past answer: TextBox allow only (HH:mm tt) format using asp.net[^].

—SA
 
Share this answer
 
To add to what Solution 1 and 2 have pointed out, you could also use DateTime.TryParse()[^] which attempts to parse it and if it fails then instead of throwing an exception like you are getting it will return a false at which point you can then handle it accordingly.
 
Share this answer
 
Here you go to follow single culture in common.

http://msdn.microsoft.com/en-us/library/bz9tc508.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900