Converting the date in asp:textbox/textmode from yyyy-mm-dd to dd-mm-yyyy?
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" />
samibravo
Updated on April 25, 2020Comments
-
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 about 10 yearsa date is a date,SQL does not care about the format
-
mason about 10 years@meda As long as the date is a DateTime! Samibravo, what column type are you using?
-
MethodMan about 10 yearsi use Telerik and format my aspx code as follows does the control have a
DateInput
property if so set if and set it'sDateFormat=dd/MM/yyyy
-
samibravo about 10 years@mason column type is
varchar
, i cant change it todatetime
at this point as the database is too big. -
samibravo about 10 years@DJKRAZE nah it doesnt have a
DataInput
property
-