zoukankan      html  css  js  c++  java
  • 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));
  • 相关阅读:
    C#生成静态两方法
    ASP.NET C# 生成静态页面简单方法
    sql日期格式化
    Div+Css+JS做多个显示/隐藏内容块
    Request获取url各种信息的方法
    asp.net遍历页面所有的按钮(或控件)
    Donews.com:SpyMac.com也提供了1G的Email.
    再见 Swagger UI!国人开源了一款超好用的 API 文档生成框架,Star 4.7K+,真香!!
    面试官:new Object[5] 一共创建了几个对象?
    面试官:select......for update 会锁表还是锁行?别答错了!
  • 原文地址:https://www.cnblogs.com/wjunwei/p/6491259.html
Copyright © 2011-2022 走看看