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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM