A.B.Developer A.B.Developer - 7 months ago 17
Javascript Question

get date from datepicker in default format and convert to another format in jquery UI

I have these elements on the page (a hidden field next to a simple textbox):

<input id="start_date" type="text">

<input id="start_date_hidden" type="hidden">


I apply datePicker to textbox I wrote this :

$("#start_date").datetimepicker({
dateFormat: 'DD dd MM yy'
});


Now I want after select a date convert selected date to another format and set into hidden field. for that i wrote this :

$("#start_date").datepicker({
dateFormat: 'DD dd MM yy',
onSelect: function ( dateText, inst) {
val = $(this).datepicker({ dateFormat: 'dd-mm-yy' }).val()

$(this).next().val(val)
}
});


But this does not work and set date with same format of textbox.

How Can I do that ?

Answer

jQuery UI Datepicker supports an "alt" field and format where you can save the selected date in another format.

$("#start_date").datepicker({
    dateFormat: 'DD dd MM yy',
    altFormat: 'dd-mm-yy',
    altField: "#start_date_hidden"
});

http://api.jqueryui.com/datepicker/#option-altField