最近有個功能會進行數據的篩選因而便寫了幾個快速計算 今日,昨日,本週,上週,本月 範圍的function 以便之後遇到一樣的問題能夠直接進行復用,代碼以下:javascript
/* *獲取今日的起始和結束時間 *返回值:"起始時間,結束時間" */ function ToDayStr() { var returnStr = ""; var date = new Date(); //當前時間 var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); //var hour = date.getHours(); //var minutes = date.getMinutes(); //var second = date.getSeconds(); month = month < 10 ? "0" + month : month; day = day < 10 ? "0" + day : day; returnStr = year + "/" + month + "/" + day + " 00:00:00,"; //起始時間 returnStr += year + "/" + month + "/" + day + " 23:59:59"; //結束時間 return returnStr; } /* *獲取昨日的起始和結束時間 *返回值:"起始時間,結束時間" */ function YesterDayStr() { var date = GetDate(1, 1); //當前時間前一天 var returnStr = ""; var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); month = month < 10 ? "0" + month : month; day = day < 10 ? "0" + day : day; returnStr = year + "/" + month + "/" + day + " 00:00:00,"; //起始時間 returnStr += year + "/" + month + "/" + day + " 23:59:59"; //結束時間 return returnStr; } /* *獲取本週的起始和結束時間 *返回值:"起始時間,結束時間" */ function ThisWeekStr() { var returnStr = ""; var date = new Date(); //當前時間 var week = date.getDay(); //獲取今天星期幾 var monday = GetDate2(week - 1, 1, date); //獲取星期一 var sunday = GetDate2(7 - week, 2, date); //獲取星期天 //起始時間的年月日 var year1 = monday.getFullYear(); var month1 = monday.getMonth() + 1; var day1 = monday.getDate(); //結束時間的年月日 var year2 = sunday.getFullYear(); var month2 = sunday.getMonth() + 1; var day2 = sunday.getDate(); //處理起始時間小於10的追加"0"在前面 month1 = month1 < 10 ? "0" + month1 : month1; day1 = day1 < 10 ? "0" + day1 : day1; //處理結束時間小於10的追加"0"在前面 month2 = month2 < 10 ? "0" + month2 : month2; day2 = day2 < 10 ? "0" + day2 : day2; returnStr = year1 + "/" + month1 + "/" + day1 + " 00:00:00,"; //起始時間 returnStr += year2 + "/" + month2 + "/" + day2 + " 23:59:59"; //結束時間 return returnStr; } /* *獲取上週的起始和結束時間 *返回值:"起始時間,結束時間" */ function LastWeekStr() { var returnStr = ""; var date = new Date(); //當前時間 var week = date.getDay(); //獲取今天星期幾 var monday = GetDate2(week + 6, 1, date); //獲取上週星期一 var sunday = GetDate2(week, 1, date); //獲取上週星期天 //起始時間的年月日 var year1 = monday.getFullYear(); var month1 = monday.getMonth() + 1; var day1 = monday.getDate(); //結束時間的年月日 var year2 = sunday.getFullYear(); var month2 = sunday.getMonth() + 1; var day2 = sunday.getDate(); //處理起始時間小於10的追加"0"在前面 month1 = month1 < 10 ? "0" + month1 : month1; day1 = day1 < 10 ? "0" + day1 : day1; //處理結束時間小於10的追加"0"在前面 month2 = month2 < 10 ? "0" + month2 : month2; day2 = day2 < 10 ? "0" + day2 : day2; returnStr = year1 + "/" + month1 + "/" + day1 + " 00:00:00,"; //起始時間 returnStr += year2 + "/" + month2 + "/" + day2 + " 23:59:59"; //結束時間 return returnStr; } /* *獲取本月的起始和結束時間 *返回值:"起始時間,結束時間" */ function ThisMonthStr() { var returnStr = ""; var date = new Date(); //當前時間 var year = date.getFullYear(); var month = date.getMonth(); var min = new Date(year, month, 1); //本月月初 var max = new Date(year, month + 1, 0); //本月月底 //起始時間的年月日 var year1 = min.getFullYear(); var month1 = min.getMonth() + 1; var day1 = min.getDate(); //結束時間的年月日 var year2 = max.getFullYear(); var month2 = max.getMonth() + 1; var day2 = max.getDate(); //處理起始時間小於10的追加"0"在前面 month1 = month1 < 10 ? "0" + month1 : month1; day1 = day1 < 10 ? "0" + day1 : day1; //處理結束時間小於10的追加"0"在前面 month2 = month2 < 10 ? "0" + month2 : month2; day2 = day2 < 10 ? "0" + day2 : day2; returnStr = year1 + "/" + month1 + "/" + day1 + " 00:00:00,"; //起始時間 returnStr += year2 + "/" + month2 + "/" + day2 + " 23:59:59"; //結束時間 return returnStr; } /* *獲取當前日期前N天或後N日期(N = day) *type:1:前;2:後 */ function GetDate(day, type) { var zdate = new Date(); var edate; if (type === 1) { edate = new Date(zdate.getTime() - (day * 24 * 60 * 60 * 1000)); } else { edate = new Date(zdate.getTime() + (day * 24 * 60 * 60 * 1000)); } return edate; } /* *獲取傳入的日期前N天或後N日期(N = day) *type:1:前;2:後 *date:傳入的日期 */ function GetDate2(day, type, date) { var zdate; if (date === null || date === undefined) { zdate = new Date(); } else { zdate = date; } var edate; if (type === 1) { edate = new Date(zdate.getTime() - (day * 24 * 60 * 60 * 1000)); } else { edate = new Date(zdate.getTime() + (day * 24 * 60 * 60 * 1000)); } return edate; }
這裏面須要注意的是不要單純的在日期或時間上進行加減,注意日期的先後一般處理的都是毫秒數 ,可仿照以上代碼中的GetDate 函數思路,僅供參考,若有疑問歡迎你們留言進行交流 java