//startDateStr(2017-02-22) endDateStr(2018-02-21)
function getDateYearSub(startDateStr, endDateStr) {
var day = 24 * 60 * 60 *1000;
var sDate = new Date(Date.parse(startDateStr.replace(/-/g, "/")));
var eDate = new Date(Date.parse(endDateStr.replace(/-/g, "/")));
//得到前一天(算头不算尾)
sDate = new Date(sDate.getTime() - day);
//获得各自的年、月、日
var sY = sDate.getFullYear();
var sM = sDate.getMonth()+1;
var sD = sDate.getDate();
var eY = eDate.getFullYear();
var eM = eDate.getMonth()+1;
var eD = eDate.getDate();
if(eY > sY && sM == eM && sD == eD) {
return eY - sY;
} else {
alert("两个日期之间并非整年,请重新选择");
return 0;
}
}
JS计算两个日期之间相差的年份
最新推荐文章于 2024-08-13 16:15:07 发布