`

获取(js)本周周一 周日日期、本季度、本月、上月的开端日期、停止日期(转帖)

阅读更多

转帖地址:http://blog.csdn.net/kongjiea/article/details/46829023

1、首先来一个自己公司项目的源码:

项目需求描述: 从20150712日开始, ,

需求①:根据当前时间返回每一周 、周一~周日的日期(需返回2种格式 格式1:7月13日,格式2:2015-07-13)

需求②:返回当前时间本周的,周一早上9:00:00和周日的23:59:59

需求③:计算出当前时间离20150712日有多少周

需求④:在需求3基础上,如果大于一周,需要返回历史周的周一早上9:00:00和周日的23:59:59  (在此用二位数组)

[html] view plain copy
 
 print?
  1. function GetWeekDate(time){  
  2.     var now = new Date(time); //当前日期   
  3.     this.nowDayOfWeek = now.getDay(); //今天本周的第几天  
  4.     this.nowYear = now.getYear(); //当前年   
  5.     this.nowMonth = now.getMonth(); //月   
  6.     this.nowDay = now.getDate(); //日   
  7.     this.beginHour="09:00:00";  
  8.     this.endHour="23:59:59";  
  9.   
  10.     this.nowYear += (this.nowYear < 2000) ? 1900 : 0; //  
  11.     this.nowDayOfWeek = this.nowDayOfWeek==0?7:this.nowDayOfWeek; // 如果是周日,就变成周七  
  12. }  
  13.   
  14. GetWeekDate.prototype.date2str=function(x,y){//date2str(new Date(curTime),"yyyy-MM-dd")  
  15.     var z ={y:x.getFullYear(),M:x.getMonth()+1,d:x.getDate(),h:x.getHours(),m:x.getMinutes(),s:x.getSeconds()};    
  16.     return y.replace(/(y+|M+|d+|h+|m+|s+)/g,function(v) {return ((v.length>1?"0":"")+eval('z.'+v.slice(-1))).slice(-(v.length>2?v.length:2))});    
  17. }  
  18.   
  19. GetWeekDate.prototype.formatDate=function(date){//格局化日期:yyyy-MM-dd   
  20.     var myyear = date.getFullYear();   
  21.     var mymonth = date.getMonth()+1;   
  22.     var myweekday = date.getDate();  
  23.     //alert("formatDate"+myyear+":"+mymonth+":"+myweekday)  
  24.     if(mymonth < 10){   
  25.         mymonth = "0" + mymonth;   
  26.     }   
  27.     if(myweekday < 10){   
  28.         myweekday = "0" + myweekday;   
  29.     }   
  30.     return (myyear+"-"+mymonth + "-" + myweekday);   
  31. }  
  32.   
  33. GetWeekDate.prototype.getWeekStartDate=function(){ //获得本周的开端日期  
  34.     var weekStartDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+1);  
  35.     return this.formatDate(weekStartDate);   
  36. }  
  37.    
  38. GetWeekDate.prototype.getWeekEndDate=function(){//获得本周的停止日期  
  39.     var weekEndDate = new Date(this.nowYear, this.nowMonth, this.nowDay + (6 - this.nowDayOfWeek+1));  
  40.     return this.formatDate(weekEndDate);   
  41. }  
  42.   
  43. GetWeekDate.prototype.getAWeedkYMD=function(){//获得本周周一~周日的年月日  
  44.     var ymdArr=[];  
  45.     for (var i = 0; i < 7; i++) {  
  46.         ymdArr[i]=[];  
  47.         //ymdArr[i][0]=this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+i+1));  
  48.         ymdArr[i][0]=this.date2str(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+i+1),'yyyy-MM-dd');  
  49.         ymdArr[i][1]=this.date2str(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+i+1), 'MM月dd日');  
  50.     };  
  51.       
  52.     return ymdArr;  
  53. }  
  54.   
  55. GetWeekDate.prototype.getQishu=function(time){//获得本周是指定日期(7.12日)的第几周  
  56.     var oNDate=new Date(time); //系统当前时间  
  57.     var oEDate=new Date('2015/7/12 23:59:59');  
  58.     var diffoNDate.getTime()-oEDate.getTime();  
  59.     //console.log(diff)  
  60.     //console.log("相差天:"+diff/(24*60*60*1000))  
  61.     //console.log("相差期数:"+parseInt(diff/(24*60*60*1000))/7)  
  62.     //console.log("取整期数:"+Math.floor(parseInt(diff/(24*60*60*1000))/7))  
  63.     return (diff/(24*60*60*1000))/7;  
  64. }  
  65.   
  66. GetWeekDate.prototype.getWeeksDates=function(time){//获取历史周排行的周一到周日时间段  
  67.     var qishu = (this.qishu || this.qishu==0)?this.qishu:this.getQishu(time);  
  68.     //var qishu=this.getQishu(time);  
  69.       
  70.     var WeeksTimes=[];//存放时间的二维数组  
  71.         minYear=2015,  
  72.         minMonth=7,  
  73.         minDay=12;  
  74.     for (var i = 0; i<qishu; i++) {  
  75.         var sday;  
  76.         var eday;  
  77.         WeeksTimes[i]=[];  
  78.         if(i==0){//如果离2015-07-12只相差1期  
  79.             sday=minDay+1; //开始时间+1天  
  80.         }else{//如果离2015-07-12相差期数>1  
  81.             sday=minDay+1+7*i;  
  82.         }  
  83.         eday=minDay+7*(i+1);//结束时间+7天  
  84.         WeeksTimes[i][0]=this.formatDate(new Date(minYear,minMonth-1,sday))+" "+this.beginHour;  
  85.         WeeksTimes[i][1]=this.formatDate(new Date(minYear,minMonth-1,eday))+" "+this.endHour;  
  86.     };  
  87.     //如果是一周的周日就不要减去一期  
  88.     return WeeksTimes;  
  89.       
  90. }  
  91.   
  92. GetWeekDate.prototype.todayData=function(json){//处理tender_list_week.jsp页面  
  93.     var oQishu=$('.qishu');  
  94.         iQishu=this.qishu;//期数+1是因为,相差0期就是第1期  
  95.         oThisWeekListBtn=$('.total_list_btn'),//查看本周排行榜按钮  
  96.         sTime=this.getWeekStartDate()+" "+this.beginHour,  
  97.         eTime=this.getWeekEndDate()+" "+this.endHour;  
  98.     //1、修改期数  
  99.         //console.log("相差周:"+iQishu);  
  100.     if(/^[1-9]\d*$/.test(iQishu) || iQishu==0){//整数 +3  
  101.         oQishu.html(iQishu*1+3);   
  102.     }else{  
  103.         oQishu.html(Math.floor(iQishu)*1+4);  
  104.     }  
  105.     oThisWeekListBtn.attr("onclick","showWeekList('"+sTime+"','"+eTime+"')");//2、修改查看本周排行榜的起止时间  
  106. }  
  107.   
  108. GetWeekDate.prototype.historyData=function(time){//处理tender_list_week.jsp页面  
  109.     var oQishu=$('.qishu'),  
  110.         oDateList=$('#dateList'), //顶部导航时间  
  111.         oHistoryList=$('#history_cont'),//历史周  
  112.         aThisWeekYMD=this.getAWeedkYMD(),//本周周一~周日的年月日  
  113.         aThisWeekDates=this.getWeeksDates(time);//获取历史周排行的周一到周日时间段  
  114.         iQishu=this.qishu;  
  115.         //console.log("相差周:"+iQishu);  
  116.       
  117.     //1、修改期数,+4是加上前3期,在+1是如果间隔3期,当前就是第4期  
  118.         //如果时间23:59:59是正整数(-1),如果是0整数(3),如果是非整数(向下取整),或负数(向上取整)但是负数不考虑  
  119.     //oQishu.html(iQishu*1+3);  
  120.     if(/^[1-9]\d*$/.test(iQishu) || iQishu==0){//整数 +3  
  121.         oQishu.html(iQishu*1+3);   
  122.     }else{  
  123.         oQishu.html(Math.floor(iQishu)*1+4);  
  124.     }  
  125.       
  126.       
  127.     //2、给顶部时间追加时间  
  128.     for (var i = 0; i < aThisWeekYMD.length; i++) {  
  129.         var str='<li data-time="'+aThisWeekYMD[i][0]+'">'+aThisWeekYMD[i][1]+'</li>';  
  130.         $(str).appendTo(oDateList);  
  131.     };  
  132.       
  133.     //3、给历史周排行榜添加周期数  
  134.     if(this.qishu>1){//如果相差的期数大于1期  
  135.         //console.dir(aThisWeekDates)  
  136.         for (var j = 0; j < aThisWeekDates.length-1; j++) {  
  137.             var iQiNum=j+4;  
  138.             var str='<li onclick="showWeekList(\''+aThisWeekDates[j][0]+'\',\''+aThisWeekDates[j][1]+'\')">第'+iQiNum+'期</li>';  
  139.             $(str).prependTo(oHistoryList);  
  140.         };  
  141.     }  
  142.       
  143. }  
  144.   
  145. GetWeekDate.prototype.init=function(time){  
  146.     var iQishu=this.getQishu(time),//期数+1是因为,相差0期就是第1期  
  147.         json={};  
  148.     json.qishu=iQishu;  
  149.     this.qishu=iQishu;  
  150.   
  151.     if($('#pageType').val()=="today"){//如果是tender_list_week.jsp页面  
  152.         if(new Date(time).getTime() < new Date('2015/07/12 23:59:59').getTime()){//特殊处理时间小于20150712  
  153.             $('.qishu').html('3');  
  154.             $('.total_list_btn').attr("onclick","showWeekList('2015-07-03 09:00:00','2015-07-12 23:59:59')");  
  155.             return false;  
  156.         }  
  157.         this.todayData(json);  
  158.     }else if($('#pageType').val()=="history"){  
  159.         if(new Date(time).getTime() < new Date('2015/07/12 23:59:59').getTime()){//特殊处理时间小于20150712  
  160.             $('.qishu').html('3');  
  161.             $('#dateList').addClass('dateList2').html('<li data-time="2015-07-03">07月03日</li><li data-time="2015-07-04">07月04日</li><li data-time="2015-07-05">07月05日</li><li data-time="2015-07-06">07月06日</li><li data-time="2015-07-07">07月07日</li><li data-time="2015-07-08">07月08日</li><li data-time="2015-07-09">07月09日</li><li data-time="2015-07-10">07月10日</li><li data-time="2015-07-11">07月11日</li><li data-time="2015-07-12">07月12日</li>');  
  162.             $('#history_cont').html('<li onclick="showWeekList(\'2015-06-26 09:00:00\',\'2015-07-02 23:59:59\')">第二期</li><li onclick="showWeekList(\'2015-06-19 12:00:00\',\'2015-06-25 23:59:59\')">第一期</li>');  
  163.             return false;  
  164.         }  
  165.         this.historyData(time);  
  166.     }  
  167.     //console.dir(this.getWeeksDates(time));  
  168. };  
  169.       
  170.   
  171.   
  172.      

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

[html] view plain copy
 
 print?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8" />  
  5.     <title>Document</title>  
  6.     <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>  
  7.     <script>  
  8.     /**  
  9. * 日期范围工具类  
  10. */  
  11. var dateRangeUtil = (function () {  
  12.     /***  
  13.     * 获得当前时间  
  14.     */  
  15.     this.getCurrentDate = function () {  
  16.         return new Date('2015','07','-1');  
  17.     };  
  18.   
  19.     /***  
  20.     * 获得本周起止时间  
  21.     */  
  22.     this.getCurrentWeek = function () {  
  23.         //起止日期数组    
  24.         var startStop = new Array();  
  25.         //获取当前时间    
  26.         var currentDate = this.getCurrentDate();  
  27.         //返回date是一周中的某一天    
  28.         var week = currentDate.getDay();  
  29.         //返回date是一个月中的某一天    
  30.         var month = currentDate.getDate();  
  31.   
  32.         //一天的毫秒数    
  33.         var millisecond = 1000 * 60 * 60 * 24;  
  34.         //减去的天数    
  35.         var minusDay = week != 0 ? week - 1 : 6;  
  36.         //alert(minusDay);    
  37.         //本周 周一    
  38.         var monday = new Date(currentDate.getTime() - (minusDay * millisecond));  
  39.         //本周 周日    
  40.         var sunday = new Date(monday.getTime() + (6 * millisecond));  
  41.         //添加本周时间    
  42.         startStop.push(monday); //本周起始时间    
  43.         //添加本周最后一天时间    
  44.         startStop.push(sunday); //本周终止时间    
  45.         //返回    
  46.         return startStop;  
  47.     };  
  48.   
  49.     /***  
  50.     * 获得本月的起止时间  
  51.     */  
  52.     this.getCurrentMonth = function () {  
  53.         //起止日期数组    
  54.         var startStop = new Array();  
  55.         //获取当前时间    
  56.         var currentDate = this.getCurrentDate();  
  57.         //获得当前月份0-11    
  58.         var currentMonth = currentDate.getMonth();  
  59.         //获得当前年份4位年    
  60.         var currentYear = currentDate.getFullYear();  
  61.         //求出本月第一天    
  62.         var firstDay = new Date(currentYear, currentMonth, 1);  
  63.   
  64.   
  65.         //当为12月的时候年份需要加1    
  66.         //月份需要更新为0 也就是下一年的第一个月    
  67.         if (currentMonth == 11) {  
  68.             currentYear++;  
  69.             currentMonth = 0; //就为    
  70.         } else {  
  71.             //否则只是月份增加,以便求的下一月的第一天    
  72.             currentMonth++;  
  73.         }  
  74.   
  75.   
  76.         //一天的毫秒数    
  77.         var millisecond = 1000 * 60 * 60 * 24;  
  78.         //下月的第一天    
  79.         var nextMonthDayOne = new Date(currentYear, currentMonth, 1);  
  80.         //求出上月的最后一天    
  81.         var lastDay = new Date(nextMonthDayOne.getTime() - millisecond);  
  82.   
  83.         //添加至数组中返回    
  84.         startStop.push(firstDay);  
  85.         startStop.push(lastDay);  
  86.         //返回    
  87.         return startStop;  
  88.     };  
  89.   
  90.     /**  
  91.     * 得到本季度开始的月份  
  92.     * @param month 需要计算的月份  
  93.     ***/  
  94.     this.getQuarterSeasonStartMonth = function (month) {  
  95.         var quarterMonthStart = 0;  
  96.         var spring = 0; //春    
  97.         var summer = 3; //夏    
  98.         var fall = 6;   //秋    
  99.         var winter = 9; //冬    
  100.         //月份从0-11    
  101.         if (month < 3) {  
  102.             return spring;  
  103.         }  
  104.   
  105.         if (month < 6) {  
  106.             return summer;  
  107.         }  
  108.   
  109.         if (month < 9) {  
  110.             return fall;  
  111.         }  
  112.   
  113.         return winter;  
  114.     };  
  115.   
  116.     /**  
  117.     * 获得该月的天数  
  118.     * @param year年份  
  119.     * @param month月份  
  120.     * */  
  121.     this.getMonthDays = function (year, month) {  
  122.         //本月第一天 1-31    
  123.         var relativeDate = new Date(year, month, 1);  
  124.         //获得当前月份0-11    
  125.         var relativeMonth = relativeDate.getMonth();  
  126.         //获得当前年份4位年    
  127.         var relativeYear = relativeDate.getFullYear();  
  128.   
  129.         //当为12月的时候年份需要加1    
  130.         //月份需要更新为0 也就是下一年的第一个月    
  131.         if (relativeMonth == 11) {  
  132.             relativeYear++;  
  133.             relativeMonth = 0;  
  134.         } else {  
  135.             //否则只是月份增加,以便求的下一月的第一天    
  136.             relativeMonth++;  
  137.         }  
  138.         //一天的毫秒数    
  139.         var millisecond = 1000 * 60 * 60 * 24;  
  140.         //下月的第一天    
  141.         var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1);  
  142.         //返回得到上月的最后一天,也就是本月总天数    
  143.         return new Date(nextMonthDayOne.getTime() - millisecond).getDate();  
  144.     };  
  145.   
  146.     /**  
  147.     * 获得本季度的起止日期  
  148.     */  
  149.     this.getCurrentSeason = function () {  
  150.         //起止日期数组    
  151.         var startStop = new Array();  
  152.         //获取当前时间    
  153.         var currentDate = this.getCurrentDate();  
  154.         //获得当前月份0-11    
  155.         var currentMonth = currentDate.getMonth();  
  156.         //获得当前年份4位年    
  157.         var currentYear = currentDate.getFullYear();  
  158.         //获得本季度开始月份    
  159.         var quarterSeasonStartMonth = this.getQuarterSeasonStartMonth(currentMonth);  
  160.         //获得本季度结束月份    
  161.         var quarterSeasonEndMonth = quarterSeasonStartMonth + 2;  
  162.   
  163.         //获得本季度开始的日期    
  164.         var quarterSeasonStartDate = new Date(currentYear, quarterSeasonStartMonth, 1);  
  165.         //获得本季度结束的日期    
  166.         var quarterSeasonEndDate = new Date(currentYear, quarterSeasonEndMonth, this.getMonthDays(currentYear, quarterSeasonEndMonth));  
  167.         //加入数组返回    
  168.         startStop.push(quarterSeasonStartDate);  
  169.         startStop.push(quarterSeasonEndDate);  
  170.         //返回    
  171.         return startStop;  
  172.     };  
  173.   
  174.     /***  
  175.     * 得到本年的起止日期  
  176.     *   
  177.     */  
  178.     this.getCurrentYear = function () {  
  179.         //起止日期数组    
  180.         var startStop = new Array();  
  181.         //获取当前时间    
  182.         var currentDate = this.getCurrentDate();  
  183.         //获得当前年份4位年    
  184.         var currentYear = currentDate.getFullYear();  
  185.   
  186.         //本年第一天    
  187.         var currentYearFirstDate = new Date(currentYear, 0, 1);  
  188.         //本年最后一天    
  189.         var currentYearLastDate = new Date(currentYear, 11, 31);  
  190.         //添加至数组    
  191.         startStop.push(currentYearFirstDate);  
  192.         startStop.push(currentYearLastDate);  
  193.         //返回    
  194.         return startStop;  
  195.     };  
  196.   
  197.     /**  
  198.     * 返回上一个月的第一天Date类型  
  199.     * @param year 年  
  200.     * @param month 月  
  201.     **/  
  202.     this.getPriorMonthFirstDay = function (year, month) {  
  203.         //年份为0代表,是本年的第一月,所以不能减    
  204.         if (month == 0) {  
  205.             month = 11; //月份为上年的最后月份    
  206.             year--; //年份减1    
  207.             return new Date(year, month, 1);  
  208.         }  
  209.         //否则,只减去月份    
  210.         month--;  
  211.         return new Date(year, month, 1); ;  
  212.     };  
  213.   
  214.     /**  
  215.     * 获得上一月的起止日期  
  216.     * ***/  
  217.     this.getPreviousMonth = function () {  
  218.         //起止日期数组    
  219.         var startStop = new Array();  
  220.         //获取当前时间    
  221.         var currentDate = this.getCurrentDate();  
  222.         //获得当前月份0-11    
  223.         var currentMonth = currentDate.getMonth();  
  224.         //获得当前年份4位年    
  225.         var currentYear = currentDate.getFullYear();  
  226.         //获得上一个月的第一天    
  227.         var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth);  
  228.         //获得上一月的最后一天    
  229.         var priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), this.getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth()));  
  230.         //添加至数组    
  231.         startStop.push(priorMonthFirstDay);  
  232.         startStop.push(priorMonthLastDay);  
  233.         //返回    
  234.         return startStop;  
  235.     };  
  236.   
  237.   
  238.     /**  
  239.     * 获得上一周的起止日期  
  240.     * **/  
  241.     this.getPreviousWeek = function () {  
  242.         //起止日期数组    
  243.         var startStop = new Array();  
  244.         //获取当前时间    
  245.         var currentDate = this.getCurrentDate();  
  246.         //返回date是一周中的某一天    
  247.         var week = currentDate.getDay();  
  248.         //返回date是一个月中的某一天    
  249.         var month = currentDate.getDate();  
  250.         //一天的毫秒数    
  251.         var millisecond = 1000 * 60 * 60 * 24;  
  252.         //减去的天数    
  253.         var minusDay = week != 0 ? week - 1 : 6;  
  254.         //获得当前周的第一天    
  255.         var currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay));  
  256.         //上周最后一天即本周开始的前一天    
  257.         var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond);  
  258.         //上周的第一天    
  259.         var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6));  
  260.   
  261.         //添加至数组    
  262.         startStop.push(priorWeekFirstDay);  
  263.         startStop.push(priorWeekLastDay);  
  264.   
  265.         return startStop;  
  266.     };  
  267.   
  268.     /**  
  269.     * 得到上季度的起始日期  
  270.     * year 这个年应该是运算后得到的当前本季度的年份  
  271.     * month 这个应该是运算后得到的当前季度的开始月份  
  272.     * */  
  273.     this.getPriorSeasonFirstDay = function (year, month) {  
  274.         var quarterMonthStart = 0;  
  275.         var spring = 0; //春    
  276.         var summer = 3; //夏    
  277.         var fall = 6;   //秋    
  278.         var winter = 9; //冬    
  279.         //月份从0-11    
  280.         switch (month) {//季度的其实月份    
  281.             case spring:  
  282.                 //如果是第一季度则应该到去年的冬季    
  283.                 year--;  
  284.                 month = winter;  
  285.                 break;  
  286.             case summer:  
  287.                 month = spring;  
  288.                 break;  
  289.             case fall:  
  290.                 month = summer;  
  291.                 break;  
  292.             case winter:  
  293.                 month = fall;  
  294.                 break;  
  295.   
  296.         };  
  297.   
  298.         return new Date(year, month, 1);  
  299.     };  
  300.   
  301.     /**  
  302.     * 得到上季度的起止日期  
  303.     * **/  
  304.     this.getPreviousSeason = function () {  
  305.         //起止日期数组    
  306.         var startStop = new Array();  
  307.         //获取当前时间    
  308.         var currentDate = this.getCurrentDate();  
  309.         //获得当前月份0-11    
  310.         var currentMonth = currentDate.getMonth();  
  311.         //获得当前年份4位年    
  312.         var currentYear = currentDate.getFullYear();  
  313.         //上季度的第一天    
  314.         var priorSeasonFirstDay = this.getPriorSeasonFirstDay(currentYear, currentMonth);  
  315.         //上季度的最后一天    
  316.         var priorSeasonLastDay = new Date(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2, this.getMonthDays(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2));  
  317.         //添加至数组    
  318.         startStop.push(priorSeasonFirstDay);  
  319.         startStop.push(priorSeasonLastDay);  
  320.         return startStop;  
  321.     };  
  322.   
  323.     /**  
  324.     * 得到去年的起止日期  
  325.     * **/  
  326.     this.getPreviousYear = function () {  
  327.         //起止日期数组    
  328.         var startStop = new Array();  
  329.         //获取当前时间    
  330.         var currentDate = this.getCurrentDate();  
  331.         //获得当前年份4位年    
  332.         var currentYear = currentDate.getFullYear();  
  333.         currentYear--;  
  334.         var priorYearFirstDay = new Date(currentYear, 0, 1);  
  335.         var priorYearLastDay = new Date(currentYear, 11, 1);  
  336.         //添加至数组    
  337.         startStop.push(priorYearFirstDay);  
  338.         startStop.push(priorYearLastDay);  
  339.         return startStop;  
  340.     };  
  341.     return this;  
  342.       
  343. })();  
  344.   
  345. $(function(){  
  346.   
  347.     $("#startDate").html( dateRangeUtil.getCurrentWeek() );  
  348. })  
  349.   
  350. </script>  
  351. </head>  
  352. <body>  
  353.     <div id="startDate"></div>  
  354. </body>  
  355. </html>  

 

 

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

 

[html] view plain copy
 
 print?
  1. Js代码     
  2. /**   
  3. * 获取本周、本季度、本月、上月的开端日期、停止日期   
  4. */   
  5. var now = new Date(); //当前日期   
  6. var nowDayOfWeek = now.getDay(); //今天本周的第几天   
  7. var nowDay = now.getDate(); //当前日   
  8. var nowMonth = now.getMonth(); //当前月   
  9. var nowYear = now.getYear(); //当前年   
  10. nowYear += (nowYear < 2000) ? 1900 : 0; //  
  11.   
  12. var lastMonthDate = new Date(); //上月日期   
  13. lastMonthDate.setDate(1);   
  14. lastMonthDate.setMonth(lastMonthDate.getMonth()-1);   
  15. var lastYear = lastMonthDate.getYear();   
  16. var lastMonth = lastMonthDate.getMonth();  
  17.   
  18. //格局化日期:yyyy-MM-dd   
  19. function formatDate(date) {   
  20. var myyear = date.getFullYear();   
  21. var mymonth = date.getMonth()+1;   
  22. var myweekday = date.getDate();  
  23.   
  24. if(mymonth < 10){   
  25. mymonth = "0" + mymonth;   
  26. }   
  27. if(myweekday < 10){   
  28. myweekday = "0" + myweekday;   
  29. }   
  30. return (myyear+"-"+mymonth + "-" + myweekday);   
  31. }  
  32.   
  33. //获得某月的天数   
  34. function getMonthDays(myMonth){   
  35. var monthStartDate = new Date(nowYear, myMonth, 1);   
  36. var monthEndDate = new Date(nowYear, myMonth + 1, 1);   
  37. var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);   
  38. return days;   
  39. }  
  40.   
  41. //获得本季度的开端月份   
  42. function getQuarterStartMonth(){   
  43. var quarterStartMonth = 0;   
  44. if(nowMonth<3){   
  45. quarterStartMonth = 0;   
  46. }   
  47. if(2<nowMonth && nowMonth<6){   
  48. quarterStartMonth = 3;   
  49. }   
  50. if(5<nowMonth && nowMonth<9){   
  51. quarterStartMonth = 6;   
  52. }   
  53. if(nowMonth>8){   
  54. quarterStartMonth = 9;   
  55. }   
  56. return quarterStartMonth;   
  57. }  
  58.   
  59. //获得本周的开端日期   
  60. function getWeekStartDate() {   
  61. var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);   
  62. return formatDate(weekStartDate);   
  63. }  
  64.   
  65. //获得本周的停止日期   
  66. function getWeekEndDate() {   
  67. var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));   
  68. return formatDate(weekEndDate);   
  69. }  
  70.   
  71. //获得本月的开端日期   
  72. function getMonthStartDate(){   
  73. var monthStartDate = new Date(nowYear, nowMonth, 1);   
  74. return formatDate(monthStartDate);   
  75. }  
  76.   
  77. //获得本月的停止日期   
  78. function getMonthEndDate(){   
  79. var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));   
  80. return formatDate(monthEndDate);   
  81. }  
  82.   
  83. //获得上月开端时候   
  84. function getLastMonthStartDate(){   
  85. var lastMonthStartDate = new Date(nowYear, lastMonth, 1);   
  86. return formatDate(lastMonthStartDate);   
  87. }  
  88.   
  89. //获得上月停止时候   
  90. function getLastMonthEndDate(){   
  91. var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));   
  92. return formatDate(lastMonthEndDate);   
  93. }  
  94.   
  95. //获得本季度的开端日期   
  96. function getQuarterStartDate(){  
  97.   
  98. var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);   
  99. return formatDate(quarterStartDate);   
  100. }  
  101.   
  102. //或的本季度的停止日期   
  103. function getQuarterEndDate(){   
  104. var quarterEndMonth = getQuarterStartMonth() + 2;   
  105. var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));   
  106. return formatDate(quarterStartDate);   
  107. }   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics