js 相差年、月、日


      <!-- 相差天数--> function datedifference(startDate, endDate) { var dateSpan, tempDate, iDays; sDate1 = Date.parse(startDate); sDate2 = Date.parse(endDate); dateSpan = sDate2 - sDate1; dateSpan = Math.abs(dateSpan); iDays = Math.floor(dateSpan / (24 * 3600 * 1000)); return iDays     }; <!-- 相差月数或者年数--> function monthdifference(startDate, endDate) { let a = new Date(startDate); let b = new Date(endDate); //年 let year = b.getFullYear() - a.getFullYear(); //月 let month = b.getMonth() - a.getMonth()+1; return month    };

  说明:开始时间和结束时间是字符串类型