js calculate the number of days between two string dates

To solve the idea, first convert the string to the millisecond value, calculate the difference, and then convert it to the number of days

<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>

 

Guess you like

Origin www.cnblogs.com/alphajuns/p/12722355.html