JS获取本周、上月、本月、上月的开端日期、停止日期

        /**
         * 获取本周、上月、本月、上月的开端日期、停止日期
         */
        var now = new Date(); //当前日期
        var nowDayOfWeek = now.getDay(); //今天本周的第几天
        var nowDay = now.getDate(); //当前日
        var nowMonth = now.getMonth(); //当前月
        var nowYear = now.getYear(); //当前年
        nowYear += (nowYear < 2000) ? 1900 : 0; //
        var nowDate = new Date(nowYear, nowMonth, nowDay);
        console.log(formatDate(nowDate));
        $scope.nowMyTime = formatDate(nowDate);

        //上月日期
        var lastMonthDate = new Date();
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
        var lastMonth = lastMonthDate.getMonth();

        //获得本周的开端日期
        var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
        $scope.weekStart = formatDate(weekStartDate);
        console.log("本周开始时间:" + formatDate(weekStartDate));
        //获得本周的停止日期
        var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) + 1);
        $scope.weekEnd = formatDate(weekEndDate);
        console.log("本周结束时间:" + formatDate(weekEndDate));
        //获得上周的开端日期
        var lastWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1 - 7);
        $scope.lastWeekStart = formatDate(lastWeekStartDate);
        console.log("上周开始时间:" + formatDate(lastWeekStartDate));
        //获得上周的停止日期
        var lastWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) + 1 - 7);
        $scope.lastWeekEnd = formatDate(lastWeekEndDate);
        console.log("上周结束时间:" + formatDate(lastWeekEndDate));
        //获得本月的开端日期
        var monthStartDate = new Date(nowYear, nowMonth, 1);
        $scope.monthStart = formatDate(monthStartDate);
        console.log("本月开始时间:" + formatDate(monthStartDate));
        //获得本月的停止日期
        var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
        $scope.monthEnd = formatDate(monthEndDate);
        console.log("本月结束时间:" + formatDate(monthEndDate));
        //获得上月开端时候
        var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
        $scope.lastMonthStart = formatDate(lastMonthStartDate);
        console.log("上月开始时间:" + formatDate(lastMonthStartDate));
        //获得上月停止时候
        var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
        $scope.lastMonthEnd = formatDate(lastMonthEndDate);
        console.log("上月结束时间:" + formatDate(lastMonthEndDate));

 

转载于:https://www.cnblogs.com/wjunwei/p/6491259.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值