js 获取本周、上周、本月、上月方法
1 /** 2 * Created by Administrator on 2015/12/11. 3 */ 4 dateRange = { 5 currentDate: new Date(), 6 millisecond: 1000 * 60 * 60 * 24,//一天的毫秒数 7 getThisWeek: function() { 8 //起止日期数组 9 var startStop = new Array(); 10 //返回date是一周中的某一天 11 var week = dateRange.currentDate.getDay(); 12 //减去的天数 13 var minusDay = week != 0 ? week - 1 : 6; 14 //本周 周一 15 var monday = new Date(dateRange.currentDate.getTime() - (minusDay * dateRange.millisecond)); 16 //本周 周日 17 var sunday = new Date(monday.getTime() + (6 * dateRange.millisecond)); 18 //添加本周时间 19 startStop.push(dateRange.formatterDate(monday)); //本周起始时间 20 //添加本周最后一天时间 21 startStop.push(dateRange.formatterDate(sunday)); //本周终止时间 22 //返回 23 return startStop; 24 }, 25 /** 26 * 获得上一周的起止日期 27 */ 28 getLastWeek: function() { 29 //起止日期数组 30 var startStop = new Array(); 31 //返回date是一周中的某一天 32 var week = dateRange.currentDate.getDay(); 33 //减去的天数 34 var minusDay = week != 0 ? week - 1 : 6; 35 //获得当前周的第一天 36 var currentWeekDayOne = new Date(dateRange.currentDate.getTime() - (dateRange.millisecond * minusDay)); 37 //上周最后一天即本周开始的前一天 38 var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - dateRange.millisecond); 39 //上周的第一天 40 var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (dateRange.millisecond * 6)); 41 42 startStop.push(dateRange.formatterDate(priorWeekFirstDay)); 43 startStop.push(dateRange.formatterDate(priorWeekLastDay)); 44 //返回 45 return startStop; 46 }, 47 /** 48 * 获得这个月的起止日期 49 */ 50 getThisMonth: function() { 51 var startStop = new Array(); 52 //获得当前月份0-11 53 var currentMonth = dateRange.currentDate.getMonth(); 54 //获得当前年份4位年 55 var currentYear = dateRange.currentDate.getFullYear(); 56 //求出本月第一天 57 var firstDay = new Date(currentYear, currentMonth, 1); 58 //当为12月的时候年份需要加1 59 //月份需要更新为0 也就是下一年的第一个月 60 //否则只是月份增加,以便求的下一月的第一天 61 if (currentMonth == 11) { 62 currentYear++; 63 currentMonth = 0; 64 } else { 65 currentMonth++; 66 } 67 //下月的第一天 68 var nextMonthDayOne = new Date(currentYear, currentMonth, 1); 69 //求出上月的最后一天 70 var lastDay = new Date(nextMonthDayOne.getTime() - dateRange.millisecond); 71 72 startStop.push(dateRange.formatterDate(firstDay)); 73 startStop.push(dateRange.formatterDate(lastDay)); 74 //返回 75 return startStop; 76 }, 77 /** 78 * 获得上个月的起止日期 79 */ 80 getLastMonth: function() { 81 var startStop = new Array(); 82 //获得当前月份0-11 83 var currentMonth = dateRange.currentDate.getMonth(); 84 //获得当前年份4位年 85 var currentYear = dateRange.currentDate.getFullYear(); 86 var currentDay = new Date(currentYear, currentMonth, 1); 87 //上个月的第一天 88 //年份为0代表,是本年的第一月,所以不能减 89 if (currentMonth == 0) { 90 currentMonth = 11; //月份为上年的最后月份 91 currentYear--; //年份减1 92 } 93 else { 94 currentMonth--; 95 } 96 var firstDay = new Date(currentYear, currentMonth, 1); 97 //求出上月的最后一天 98 var lastDay = new Date(currentDay.getTime() - dateRange.millisecond); 99 100 startStop.push(dateRange.formatterDate(firstDay)); 101 startStop.push(dateRange.formatterDate(lastDay)); 102 //返回 103 return startStop; 104 }, 105 /** 106 * 格式化日期(不含时间) 107 */ 108 formatterDate : function(date) { 109 var datetime = date.getFullYear() 110 + "-"// "年" 111 + ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0" 112 + (date.getMonth() + 1)) 113 + "-"// "月" 114 + (date.getDate() < 10 ? "0" + date.getDate() : date 115 .getDate()); 116 return datetime; 117 } 118 }