Converting the date in asp:textbox/textmode from yyyy-mm-dd to dd-mm-yyyy?

22,600

Solution 1

I used the Ajaxtoolkit calender, so easy and very flexible heres the code i used

<asp:TextBox ID="tb_search_date" runat="server" /> <ajaxToolkit:CalendarExtender ID="CalendarExtender1" TargetControlID="tb_search_date" Format="dd-MM-yyyy" runat="server" OnClientShown="DisableWeekends </ajaxToolkit:CalendarExtender>

You can replace the "-" in the format box to whatever you like and it will reformat the date to that.

Here a site to it

http://www.asp.net/ajaxlibrary/ajaxcontroltoolkitsamplesite/calendar/calendar.aspx

Solution 2

You can just convert it on the server side when you insert it.

DateTime theDate=DateTime.ParseExact(TextBox1.Text, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
string dateToInsert=theDate.ToString("dd-MM-yyyy");

I believe it's just using the HTML5 input of "date" type when you set the ASP TextBoxMode to date. Therefore, the format is really up to the client. That's not good, because then we don't know what date format the client might use!

A better solution would be to use something like jQuery UI DatePicker. That will give you better control over what is used no matter what client.

<script>
  $(function() {
    $( "#datepicker" ).datepicker({dateFormat: "dd-mm-yyyy"});
  });
  </script>

<asp:TextBox runat="server" id="datepicker" ClientIDMode="Static" />
Share:
22,600
samibravo
Author by

samibravo

Updated on April 25, 2020

Comments

  • samibravo
    samibravo about 4 years

    Am having trouble trying to pass a certain format of a date from a textbox.

    In asp the textbox allows you change the textmode to date.

    In doing so it give you a nice little calender pop up. The only problem am having is the format its put in is yyyy-mm-dd and i want to change it to dd-mm-yyyy so that i can pass it into a sql string

    Is there a possible way of doing this.

    Many Thanks

    • meda
      meda about 10 years
      a date is a date,SQL does not care about the format
    • mason
      mason about 10 years
      @meda As long as the date is a DateTime! Samibravo, what column type are you using?
    • MethodMan
      MethodMan about 10 years
      i use Telerik and format my aspx code as follows does the control have a DateInput property if so set if and set it's DateFormat=dd/MM/yyyy
    • samibravo
      samibravo about 10 years
      @mason column type is varchar, i cant change it to datetime at this point as the database is too big.
    • samibravo
      samibravo about 10 years
      @DJKRAZE nah it doesnt have a DataInput property