JS获取 今天 本周 本月 本年 的开始和结束时间

 formatDate(date) {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, "0");
            const day = String(date.getDate()).padStart(2, "0");
            const hours = String(date.getHours()).padStart(2, "0");
            const minutes = String(date.getMinutes()).padStart(2, "0");
            const seconds = String(date.getSeconds()).padStart(2, "0");

            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        },
        getToDay() {
            // 获取今天的开始时间和结束时间
            const today = new Date();
            const todayStartTime = new Date(
                today.getFullYear(),
                today.getMonth(),
                today.getDate(),
                0,
                0,
                0
            );
            const todayEndTime = new Date(
                today.getFullYear(),
                today.getMonth(),
                today.getDate(),
                23,
                59,
                59
            );

            return [
                this.formatDate(todayStartTime),
                this.formatDate(todayEndTime)
            ];
        },

        getCurrentWeek() {
            // 获取本周的开始时间和结束时间
            const currentWeek = new Date();
            const currentWeekFirstDay =
                currentWeek.getDate() - currentWeek.getDay() + 1;
            const currentWeekStartTime = new Date(
                currentWeek.getFullYear(),
                currentWeek.getMonth(),
                currentWeekFirstDay,
                0,
                0,
                0
            );
            const currentWeekEndTime = new Date(
                currentWeek.getFullYear(),
                currentWeek.getMonth(),
                currentWeekFirstDay + 6,
                23,
                59,
                59
            );
            return [
                this.formatDate(currentWeekStartTime),
                this.formatDate(currentWeekEndTime)
            ];
        },
        getLastMonth() {
            // 获取上月的开始时间和结束时间
            const lastMonth = new Date();
            lastMonth.setMonth(lastMonth.getMonth() - 1);
            const lastMonthFirstDay = new Date(
                lastMonth.getFullYear(),
                lastMonth.getMonth(),
                1
            );
            const lastMonthStartTime = new Date(
                lastMonthFirstDay.getFullYear(),
                lastMonthFirstDay.getMonth(),
                lastMonthFirstDay.getDate(),
                0,
                0,
                0
            );
            const lastMonthEndTime = new Date(
                lastMonth.getFullYear(),
                lastMonth.getMonth() + 1,
                0,
                23,
                59,
                59
            );

            return [
                this.formatDate(lastMonthStartTime),
                this.formatDate(lastMonthEndTime)
            ];
        },
        getLastYera() {
            //获取今年的开始和结束时间
            const now = new Date(); // 获取当前时间
            const year = now.getFullYear(); // 获取当前年份
            const startOfYear = new Date(year, 0, 1); // 创建一个新日期对象,表示本年的第一天
            const endOfYear = new Date(year, 11, 31, 23, 59, 59, 999); // 创建一个新日期对象,表示本年的最后一天
            return [this.formatDate(startOfYear), this.formatDate(endOfYear)];
        },

最后输出为数组


["2024-01-09 00:00:00","2024-01-09 23:59:59"]

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值