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 }