JS 獲取當年第幾周,當月共幾周


 1、獲取當天屬於當年中第幾周

    <script type="text/javascript">
        function getWeek(dt) {
            let d1 = new Date(dt);
            let d2 = new Date(dt);
            d2.setMonth(0);
            d2.setDate(1);
            let rq = d1 - d2;
            let days = Math.ceil(rq / (24 * 60 * 60 * 1000));
            let num = Math.ceil(days / 7);
            return num+1;
        }
        var ss = getWeek("2019-12-31")
        alert(ss);

    </script>

 2、獲取當月的周數

 1         function getMonthWeek(a, b, c) {
 2             var date = new Date(a, parseInt(b) - 1, c), w = date.getDay(), d = date.getDate();
 3             return Math.ceil(
 4             (d + 6 - w) / 7
 5             );
 6         };
 7         var last = new Date(2019, 7, 0);//獲取當前月最后一天時間
 8         var y = last.getYear();
 9         var m = last.getMonth() + 1;
10         var d = last.getDate();
11         document.write("當月最多有 ", getMonthWeek(y, m, d), " 周");

 


免責聲明!

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



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