Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to convert dd/mm/yyyy date format to mm/dd/yyyy . In my form i want to show the date to user in dd/mm/yyyy for that i have done:
C#
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
   {
       TextBox2_PopupControlExtender.Commit(Calendar1.SelectedDate.ToString("dd/MM/yyyy"));
   }

But as we know that database accepts date in mm-dd-yyyy, so on executing my project error is generated:
Quote:
String was not recognized as a valid DateTime.

I heard it somewhere that i had to split the date first and then change it to mm-dd-yyyy but don't know how to do it. Thanx.
Posted

Databases don't "accepts date in mm-dd-yyyy", they expect ISO format "yyyy-MM-dd" but will work with others if they have to.

Instead of passing string dates around, use parametrized queries to pass DateTime objects instead - it's safer, because strings are always vulnerable to SQL Injection attack, and easier because it doesn't require you to convert DateTime to string and back again at any time. Formatting a date to a string should only be done at the last moment for presentation to the user, just as strings should be converted to datetime values as soon as possible - that way, you can let teh user have his data in the format he is used to, and has his computer set to.
 
Share this answer
 
This may help you:
C#
DateTime.ParseExact(TextBox2.Text.Trim(), "dd/MM/yyyy", null).ToString("MM/dd/yyyy");

Check this[^] for more details.

--Amit
 
Share this answer
 
v2
Comments
Rambo_Raja 3-Jul-13 6:03am    
cannot implicitly convert type string to system.datetime. This error is coming when using your code..
An ugly solution but it works?

C#
s = s.Split("/")(1) + "/" + s.Split("/")(0) + "/" + s.Split("/")(2);
 
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