js 日期相差的天数
function DateDiff(sDate1, sDate2){ //sDate1和sDate2是2006-12-18格式 var aDate, oDate1, oDate2, iDays aDate = sDate1.split("-") oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-18-2006格式 aDate = sDate2.split("-") oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数 return iDays + 1 }
var day1 = (new Date).setFullYear(2011, 7, 16); var day2 = (new Date).setFullYear(2011, 7, 20); var number_of_days = (day2 - day1) / 86400000; // 输出 4 alert(number_of_days); // 将值赋予 input document.getElementById('days').value = number_of_days;