怎么样设置默认今天到一周前的时间,默认一周的时间

怎么样设置默认今天到一周前的时间,默认一周的时间怎么样设置默认今天到一周前的时间,默认一周的时间

img

img

这样不知道能不能满足你的需求。

img

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>


        function getLastWeekTime() {
            // 当前时间
            let cur = new Date();
            // 一周前
            let start = new Date((cur.getTime() - 7 * 24 * 60 * 60 * 1000));

            let start_year = start.getFullYear();
            let start_month = start.getMonth() + 1;
            let start_day = start.getDate();

            start_month = start_month > 9 ? start_month : '0' + start_month
            start_day = start_day > 9 ? start_day : '0' + start_day

            let cur_year = cur.getFullYear();
            let cur_month = cur.getMonth() + 1;
            let cur_day = cur.getDate();

            console.log(cur, start);
            cur_month = cur_month > 9 ? cur_month : ('0' + cur_month)
            cur_day = cur_day > 9 ? cur_day : ('0' + cur_day)


            return {
                startDate: `${start_year}-${start_month}-${start_day} 00:00:00`,
                endDate: `${cur_year}-${cur_month}-${cur_day} 23:59:59`,
            }
        }


        console.log(getLastWeekTime());

    </script>
</body>

</html>

可以获取当天时间的时间戳,然后你在这个时间戳减去606024*7就是一周前的时间戳
然后将时间戳转换成年月日

https://blog.csdn.net/LavanSum/article/details/72846078
如果一定要前端自己设置参考这个,
建议通过后端获取,这样不管是什么默认的时间都好做,而且后期调整也容易