<scripttype="text/javascript"> // var d = getDateYearSub("2017-02-22","2018-02-22"); // console.log(d); functiongetDateYearSubNew() { // debugger; var day = 24 * 60 * 60 * 1000;
var startDateStr = document.getElementsByName("startDate")[0].value; var endDateStr = document.getElementsByName("endDate")[0].value;
var sDate = newDate(Date.parse(startDateStr.replace(/-/g, "/"))); var eDate = newDate(Date.parse(endDateStr.replace(/-/g, "/")));
var result = document.getElementsByName("result")[0];
//获得各自的年、月、日 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();
var yL = eY - sY; var mL = eM - sM; var dL = eD - sD; console.log("年相差:" + yL); console.log("月相差:" + mL); console.log("日相差:" + dL);
if (dL < 0) { mL--; } if (mL < 0) { yL--; } if (yL > 0) { result.value = yL; }else{ result.value = 0; } } </script>