• 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));
  • 相关阅读:
    echarts 点击事件 点击一次触发多次
    echarts 没有数据时,无数据展示
    递归 出现的一些问题
    echarts 下钻 返回设成按钮的样式
    阻止冒泡 阻止默认事件
    前端如何绘制svg格式图片
    html头部的标签有哪些?
    ant design 封装可填写Tag标签的input框
    React:通过Vite集成Tailwind CSS
    React:通过vite批量导入图片
  • 原文地址:https://www.cnblogs.com/wjunwei/p/6491259.html
Copyright © 2020-2023  润新知