扩展Date的DateDiff方法--日期差

时间:2023-03-08 16:38:21
Date.prototype.DateDiff = function(after){
var diffDay;
var beforeDate = new Date(this).format("yyyy-MM-dd");
beforeDate = beforeDate.split('-');
beforeDate = new Date(beforeDate[1] + '-' + beforeDate[2] + '-' + beforeDate[0]).toLong();
console.log("beforeDate");
console.log(beforeDate);
var afterDate = new Date(after).format("yyyy-MM-dd");
afterDate = afterDate.split("-");
afterDate = new Date(afterDate[1] + '-' + afterDate[2] + '-' + afterDate[0]).toLong();
console.log("afterDate");
console.log(afterDate);
diffDay = parseInt(Math.abs(afterDate - beforeDate) / 1000 / 60 / 60 /24);
console.log("diffDay");
console.log(diffDay);
return diffDay + 1 +"天";
};