js计算两个日期的天数差

js计算两个日期的天数差

//计算两个日期的天数差
function dateDiff(firstDate,secondDate){
var firstDate = new Date(firstDate);
var secondDate = new Date(secondDate);
var diff = Math.abs(firstDate.getTime() - secondDate.getTime())
var result = parseInt(diff / (1000 * 60 * 60 * 24));
return result
}

//将日期转换为yyyy-mm-dd的格式
function formatDate(date) {
var d = new Date(date),
month = ‘’ + (d.getMonth() + 1),
day = ‘’ + d.getDate(),
year = d.getFullYear();

if (month.length < 2) month = ‘0’ + month;
if (day.length < 2) day = ‘0’ + day;
return [year, month, day].join(’-’);
}

你可能感兴趣的:(js开发)