Search code examples
asp.netajaxdatetimecalendarextender

Ajax CalendarExtender giving the wrong format asp.net


I'm using an Ajax CalendarExtender attached to a textbox in my page. I am able to get the date at the correct format when the textbox to which it is attached is Null at the begining . But then i load date values from my database, and this time i want to get the new date selected in the calendar to update it in my database. for this purpose i use a javascript function to retrieve this date and store it in a hiddenfield so i could use it in code behind later. problem is the dates retrieved are correct, but in adifferent format the calendar format .

here is my aspx code :

 <asp:TextBox ID="TextBox2" runat="server" Width="124px" ReadOnly=false
                    ontextchanged="TextBox2_TextChanged"  OnChange="SetHiddenVariable1()" ></asp:TextBox>

                <ajaxToolkit:CalendarExtender ID="TextBox2_CalendarExtender" BehaviorID="TextBox2_CalendarExtender" runat="server" 
                    TargetControlID="TextBox2" Format="dd/MM/yyyy" Enabled="True"  >
                </ajaxToolkit:CalendarExtender>

and here is the javascript used:

function SetHiddenVariable1() {
    var hiddenControl1 = '<%= HiddenField_date_debut.ClientID %>';
    document.getElementById(hiddenControl1).value = $find("TextBox2_CalendarExtender").get_selectedDate();
}

the result in HiddenField_dateçdebut:

when i select 30/07/2013 in the calendar i get Tue Jul 30 2013 02:00:00 GMT+0200 (Paris, Madrid (heure d’été)) in the hidden field

How Can I store this value in the hiddenfield in the correct format? Thank You.


Solution

  • If you want to try and fix just the formatting do the following by adding .format("dd/MM/yyyy") at the end

     $find("TextBox2_CalendarExtender").get_selectedDate().format("dd/MM/yyyy");