js计算2个日期之间相差多少天

计算2个日期之间相差多少天

    <script>
        function getDays(strDateStart, strDateEnd) {
            var strSeparator = "-";
            //日期分隔符   var oDate1;   var oDate2;   
            var iDays;
            oDate1 = strDateStart.split(strSeparator);
            oDate2 = strDateEnd.split(strSeparator);
            var strDateS = new Date(oDate1[0], oDate1[1] - 1, oDate1[2]);
            var strDateE = new Date(oDate2[0], oDate2[1] - 1, oDate2[2]);
            iDays = parseInt(Math.abs(strDateS - strDateE) / 1000 / 60 / 60 / 24)
            //把相差的毫秒数转换为天数   
            return iDays;
        }
        console.log((getDays('2021-11-08','2020-11-08')));
    </script>
posted @ 2021-11-12 17:05  江咏之  阅读(1530)  评论(0编辑  收藏  举报