你的意思是这样的吗?
function days() {
var a = new Date($("#datepicker_start").val()),
b = new Date($("#datepicker_end").val()),
c = 24*60*60*1000,
diffDays = Math.round(Math.abs((a - b)/(c)));
$("#totaldays").val(diffDays);
}
function formatDate(_d){
var d = new Date(_d);
var curr_date = d.getDate();
if(curr_date < 10)
curr_date = "0" + curr_date;
var curr_month = d.getMonth() + 1; //Months are zero based
if(curr_month < 10)
curr_month = "0" + curr_month;
var curr_year = d.getFullYear();
return curr_month + '/' + curr_date + '/' + curr_year;
}
$('#totaldays').change(function(){
if($(this).val() != ''){
var days = $(this).val();
var start= new Date($("#datepicker_start").val());
var newStart = start.setDate(start.getDate() + parseInt(days));
$("#datepicker_end").val(formatDate(newStart));
}else{
$("#datepicker_end").val($("#datepicker_start").val());
}
});
$('.datepicker')
.datepicker({format: 'mm/dd/yyyy'})
.on('changeDate', function(ev){
days();
$(this).datepicker('hide').blur();
});
演示:
http://jsfiddle.net/dirtyd77/KLpq7/218/