解决思路,先将字符串转化为毫秒值,计算差值,然后转化为天数
<script> window.onload = function() { var str1 = "2020-04-17"; var str2 = "2020-04-12"; var day1 = new Date(str1); var day2 = new Date(str2); //console.log("day1:" + day1); //console.log("day2:" + day2); var differDay = Math.abs(day1-day2)/1000/60/60/24; console.log(differDay); } </script>