求前年月日
var datetime = new Date();
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
var date = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate();
var newTime = year+'-'+month+'-'+date;
求相差月份
function caluDiffMonth (startDateTime, endDateTime){
var startTime = new Date(startDateTime); // 开始时间
var endTime = new Date(endDateTime); // 结束时间
debugger
var usedTime = endTime - startTime; // 相差的毫秒数
var Months = (Math.floor(usedTime / (24 * 3600 * 1000))) / 30;
Months = 1*Months.toFixed(0);
return Months;
}
caluDiffMonth("2021-06-08","2022-06-08") 相差12个月