美文网首页
js获取周数据用作周期选择下拉框

js获取周数据用作周期选择下拉框

作者: 忧郁的冥 | 来源:发表于2019-07-23 11:01 被阅读0次
    <html>
    <title>测试</title>
    <body>
    <script>
        var year = 2019 //选择年份
        var lastDay = new Date(year + 1,0,0) // 一年的最后一天
        var firstDay = new Date(year,0,1) // 一年的第一天
        console.log(lastDay);
        console.log(firstDay);
        var obj = {}
        var index = 1;
       // var day = firstDay;
        var day = new Date(year,0,1);
        function formatDate(d){
            return d.toLocaleDateString('zh-cn',{year: 'numeric', month: '2-digit',day: '2-digit'}).replace(/\//g,'-')
        }
        while (day < lastDay){
            day.setDate(day.getDate() + 1)
            if(day.getDay() === 0){
                obj['第' + (index++) + '周'] = formatDate(firstDay) + ' 到 ' + formatDate(day)
                firstDay = new Date(day.getTime() + 86400000)
            }
        }
        if(day !== lastDay){
            obj['第' + (index++) + '周'] = formatDate(firstDay) + ' 到 ' + formatDate(lastDay)
        }
        console.log(JSON.stringify(obj,null,' '))
    </script>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:js获取周数据用作周期选择下拉框

          本文链接:https://www.haomeiwen.com/subject/igqolctx.html