I have 2 datepickers in jquery where I add a single day to the date of one picker and assign it to another date picker like so:
/*assign date pickers*/
$("#MainContent_txtActualShipDate").datepicker({
dateFormat: 'mm/dd-yyyy',
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true
});
$("#MainContent_txtExpectedShipDate").datepicker({
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function (selectedDate) {
var date = $(this).datepicker('getDate');
if (date) {
date.setDate(date.getDate() + 1);
$("#MainContent_txtActualShipDate").val(date);
}
}
});
Notice the onSelect function adds a single date and assigns it to #MainContent_txtActualShipDate but the value it gets is “TUE APR 24 2012 00:00:00”.
I want it to be simply ’04/24/2012′ so mm/dd/yyyy. I read the jquery ui docs and looked up some examples on stackoverflow but none of them worked. Is there anyone that can look at this code and edit to get the format I want?
Edit
Here is what I change it to:
$("#MainContent_txtExpectedShipDate").datepicker({
dateFormat: 'mm/dd-yyyy',
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function (selectedDate) {
var date = $(this).datepicker('getDate');
if (date) {
date.setDate(date.getDate() + 1);
var formattedDate = $.datepicker.formatDate('mm/dd-yyyy', date);
$("#MainContent_txtActualShipDate").val(formattedDate);
// $("#MainContent_txtActualShipDate").val(date);
}
}
});
But it shows the year twice 04/12/20122012.
Answer
Here is what worked for others in the future:
/*assign date pickers*/
$("#MainContent_txtActualShipDate").datepicker({
dateFormat: 'mm/dd/yy',
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true
});
$("#MainContent_txtExpectedShipDate").datepicker({
dateFormat: 'mm/dd/yy',
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function (selectedDate) {
var date = $(this).datepicker('getDate');
if (date) {
date.setDate(date.getDate() + 1);
var formattedDate = $.datepicker.formatDate('mm/dd/yy', date);
$("#MainContent_txtActualShipDate").val(formattedDate);
}
}
});
/*end assign date pickers*/
Thanks
Use the
formatDate()function: