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));

 

posted @ 2017-03-02 16:27  wjw_Dream  阅读(1832)  评论(0编辑  收藏  举报