美文网首页
js动态获取本周七天的日期

js动态获取本周七天的日期

作者: 帆_44fc | 来源:发表于2019-12-20 10:40 被阅读0次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>获取本周日期</title>
</head>

<body>
    <script>
        // 所需数据格式
        // {
        //     id: 1,
        //     name: "周一",
        //     job: "12月16日"
        //   },

        // 获取本周七天日期
        function getWeekDay(dateString) {
            let dateStringReg = /^\d{4}[/-]\d{1,2}[/-]\d{1,2}$/;
            if (dateString.match(dateStringReg)) {
                let presentDate = new Date(dateString),
                    today = presentDate.getDay() !== 0 ? presentDate.getDay() : 7;
                return Array.from(new Array(7), function (val, index) {
                    return formatDate(new Date(presentDate.getTime() - (today - index - 1) * 24 * 60 * 60 *
                        1000));
                });
            } else {
                throw new Error('dateString should be like "yyyy-mm-dd" or "yyyy/mm/dd"');
            }
            function formatDate(date) {
                return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate()+'日';
            }
        }

        var data=getWeekDay('2019-12-9');
      
       // var arr=[];
       // var weekday = [ "星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期日"];

       // data.map((val,index)=>{
            //arr.push({
               // id:index,
               // name:weekday[index],
               // job:val
            //})
      //  });
      
    </script>
</body>

</html>

相关文章

网友评论

      本文标题:js动态获取本周七天的日期

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