0

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

Community
  • 1
  • 1
Zoya
  • 405
  • 3
  • 10
  • 21

3 Answers3

2

You can use:

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

ex:

DateTime dt =DateTime.ParseExact("yourDate","formatinWhichYouWant",culture of current string);
  • yes this is the best way to convert date I have also tried to find some other more precise manner but not able to find any one –  Jul 30 '12 at 11:08
0
string myDate = bapur.Text.Split("/");
string dateString = myDate[1] + "/" + myDate[0] + "/" + myDate[2];
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

got my answer

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

            Bapur.Date = newDate.ToString();
Zoya
  • 405
  • 3
  • 10
  • 21
  • upvoted for constant search and effort in finding answer after asking the question –  Jul 30 '12 at 11:35