samibravo
samibravo

Reputation: 23

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

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

Upvotes: 0

Views: 27999

Answers (2)

samibravo
samibravo

Reputation: 23

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

Upvotes: 0

mason
mason

Reputation: 32704

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

Upvotes: 3

Related Questions