I m using ajax calendar control to a textbox, have set its format as "dd/mm/yyyy" .

  Bapur.Date = txtdate.Text;

in data access layer

 cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = bapur.Date;

on saving, like above ( where Date is a string Bapur is object of businesslayer class) in the database where datatable in database has date as datetime format. m getting an error : cant convert string to datetime i dint get error when format was "mm/dd/yyyy" .

Basically, I want users to view date in dd/mm/yyyy but on saving i want it in

mm/dd/yyyy

Have tried a lot but not working.

here is my answer ---- https://stackoverflow.com/a/11720162/1445836 -----

3

There are 3 best solutions below

1
On BEST ANSWER

got my answer

            string old = txtdate.Text;
            string newDate = DateTime.ParseExact(old, "dd/MM/yyyy", null).ToString("MM/dd/yyyy");

            Bapur.Date = newDate.ToString();
0
On
string myDate = bapur.Text.Split("/");
string dateString = myDate[1] + "/" + myDate[0] + "/" + myDate[2];
1
On

You can use:

DateTime.ParseExact("yourDate","formatinWhichYouWant",culture of current string);

ex:

DateTime dt =DateTime.ParseExact("yourDate","formatinWhichYouWant",culture of current string);