JS日期轉換格式 和 JS 獲取 今日、昨日、本週、本月、本季度、本年、上月、上週、上季度、去年
阿新 • • 發佈:2018-11-12
// 對Date的擴充套件,將 Date 轉化為指定格式的String // 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個佔位符, // 年(y)可以用 1-4 個佔位符,毫秒(S)只能用 1 個佔位符(是 1-3 位的數字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小時 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } /** * 日期範圍工具類 */ var dateRangeUtil = (function () { /*** * 獲得當前時間 */ this.getCurrentDate = function () { return new Date(); }; /*** * 獲得本週起止時間 */ this.getCurrentWeek = function () { //起止日期陣列 var startStop = new Array(); //獲取當前時間 var currentDate = this.getCurrentDate(); //返回date是一週中的某一天 var week = currentDate.getDay(); //返回date是一個月中的某一天 var month = currentDate.getDate(); //一天的毫秒數 var millisecond = 1000 * 60 * 60 * 24; //減去的天數 var minusDay = week != 0 ? week - 1 : 6; //alert(minusDay); //本週 週一 var monday = new Date(currentDate.getTime() - (minusDay * millisecond)); //本週 週日 var sunday = new Date(monday.getTime() + (6 * millisecond)); //新增本週時間 startStop.push(monday); //本週起始時間 //新增本週最後一天時間 startStop.push(sunday); //本週終止時間 //返回 return startStop; }; /*** * 獲得本月的起止時間 */ this.getCurrentMonth = function () { //起止日期陣列 var startStop = new Array(); //獲取當前時間 var currentDate = this.getCurrentDate(); //獲得當前月份0-11 var currentMonth = currentDate.getMonth(); //獲得當前年份4位年 var currentYear = currentDate.getFullYear(); //求出本月第一天 var firstDay = new Date(currentYear, currentMonth, 1); //當為12月的時候年份需要加1 //月份需要更新為0 也就是下一年的第一個月 if (currentMonth == 11) { currentYear++; currentMonth = 0; //就為 } else { //否則只是月份增加,以便求的下一月的第一天 currentMonth++; } //一天的毫秒數 var millisecond = 1000 * 60 * 60 * 24; //下月的第一天 var nextMonthDayOne = new Date(currentYear, currentMonth, 1); //求出上月的最後一天 var lastDay = new Date(nextMonthDayOne.getTime() - millisecond); //新增至陣列中返回 startStop.push(firstDay); startStop.push(lastDay); //返回 return startStop; }; /** * 得到本季度開始的月份 * @param month 需要計算的月份 ***/ this.getQuarterSeasonStartMonth = function (month) { var quarterMonthStart = 0; var spring = 0; //春 var summer = 3; //夏 var fall = 6; //秋 var winter = 9; //冬 //月份從0-11 if (month < 3) { return spring; } if (month < 6) { return summer; } if (month < 9) { return fall; } return winter; }; /** * 獲得該月的天數 * @param year年份 * @param month月份 * */ this.getMonthDays = function (year, month) { //本月第一天 1-31 var relativeDate = new Date(year, month, 1); //獲得當前月份0-11 var relativeMonth = relativeDate.getMonth(); //獲得當前年份4位年 var relativeYear = relativeDate.getFullYear(); //當為12月的時候年份需要加1 //月份需要更新為0 也就是下一年的第一個月 if (relativeMonth == 11) { relativeYear++; relativeMonth = 0; } else { //否則只是月份增加,以便求的下一月的第一天 relativeMonth++; } //一天的毫秒數 var millisecond = 1000 * 60 * 60 * 24; //下月的第一天 var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1); //返回得到上月的最後一天,也就是本月總天數 return new Date(nextMonthDayOne.getTime() - millisecond).getDate(); }; /** * 獲得本季度的起止日期 */ this.getCurrentSeason = function () { //起止日期陣列 var startStop = new Array(); //獲取當前時間 var currentDate = this.getCurrentDate(); //獲得當前月份0-11 var currentMonth = currentDate.getMonth(); //獲得當前年份4位年 var currentYear = currentDate.getFullYear(); //獲得本季度開始月份 var quarterSeasonStartMonth = this.getQuarterSeasonStartMonth(currentMonth); //獲得本季度結束月份 var quarterSeasonEndMonth = quarterSeasonStartMonth + 2; //獲得本季度開始的日期 var quarterSeasonStartDate = new Date(currentYear, quarterSeasonStartMonth, 1); //獲得本季度結束的日期 var quarterSeasonEndDate = new Date(currentYear, quarterSeasonEndMonth, this.getMonthDays(currentYear, quarterSeasonEndMonth)); //加入陣列返回 startStop.push(quarterSeasonStartDate); startStop.push(quarterSeasonEndDate); //返回 return startStop; }; /*** * 得到本年的起止日期 * */ this.getCurrentYear = function () { //起止日期陣列 var startStop = new Array(); //獲取當前時間 var currentDate = this.getCurrentDate(); //獲得當前年份4位年 var currentYear = currentDate.getFullYear(); //本年第一天 var currentYearFirstDate = new Date(currentYear, 0, 1); //本年最後一天 var currentYearLastDate = new Date(currentYear, 11, 31); //新增至陣列 startStop.push(currentYearFirstDate); startStop.push(currentYearLastDate); //返回 return startStop; }; /** * 返回上一個月的第一天Date型別 * @param year 年 * @param month 月 **/ this.getPriorMonthFirstDay = function (year, month) { //年份為0代表,是本年的第一月,所以不能減 if (month == 0) { month = 11; //月份為上年的最後月份 year--; //年份減1 return new Date(year, month, 1); } //否則,只減去月份 month--; return new Date(year, month, 1); ; }; /** * 獲得上一月的起止日期 * ***/ this.getPreviousMonth = function () { //起止日期陣列 var startStop = new Array(); //獲取當前時間 var currentDate = this.getCurrentDate(); //獲得當前月份0-11 var currentMonth = currentDate.getMonth(); //獲得當前年份4位年 var currentYear = currentDate.getFullYear(); //獲得上一個月的第一天 var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth); //獲得上一月的最後一天 var priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), this.getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth())); //新增至陣列 startStop.push(priorMonthFirstDay); startStop.push(priorMonthLastDay); //返回 return startStop; }; /** * 獲得上一週的起止日期 * **/ this.getPreviousWeek = function () { //起止日期陣列 var startStop = new Array(); //獲取當前時間 var currentDate = this.getCurrentDate(); //返回date是一週中的某一天 var week = currentDate.getDay(); //返回date是一個月中的某一天 var month = currentDate.getDate(); //一天的毫秒數 var millisecond = 1000 * 60 * 60 * 24; //減去的天數 var minusDay = week != 0 ? week - 1 : 6; //獲得當前周的第一天 var currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay)); //上週最後一天即本週開始的前一天 var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond); //上週的第一天 var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6)); //新增至陣列 startStop.push(priorWeekFirstDay); startStop.push(priorWeekLastDay); return startStop; }; /** * 得到上季度的起始日期 * year 這個年應該是運算後得到的當前本季度的年份 * month 這個應該是運算後得到的當前季度的開始月份 * */ this.getPriorSeasonFirstDay = function (year, month) { var quarterMonthStart = 0; var spring = 0; //春 var summer = 3; //夏 var fall = 6; //秋 var winter = 9; //冬 //月份從0-11 switch (month) {//季度的其實月份 case spring: //如果是第一季度則應該到去年的冬季 year--; month = winter; break; case summer: month = spring; break; case fall: month = summer; break; case winter: month = fall; break; }; return new Date(year, month, 1); }; /** * 得到上季度的起止日期 * **/ this.getPreviousSeason = function () { //起止日期陣列 var startStop = new Array(); //獲取當前時間 var currentDate = this.getCurrentDate(); //獲得當前月份0-11 var currentMonth = currentDate.getMonth(); //獲得當前年份4位年 var currentYear = currentDate.getFullYear(); //上季度的第一天 var priorSeasonFirstDay = this.getPriorSeasonFirstDay(currentYear, currentMonth); //上季度的最後一天 var priorSeasonLastDay = new Date(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2, this.getMonthDays(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2)); //新增至陣列 startStop.push(priorSeasonFirstDay); startStop.push(priorSeasonLastDay); return startStop; }; /** * 得到去年的起止日期 * **/ this.getPreviousYear = function () { //起止日期陣列 var startStop = new Array(); //獲取當前時間 var currentDate = this.getCurrentDate(); //獲得當前年份4位年 var currentYear = currentDate.getFullYear(); currentYear--; var priorYearFirstDay = new Date(currentYear, 0, 1); var priorYearLastDay = new Date(currentYear, 11, 1); //新增至陣列 startStop.push(priorYearFirstDay); startStop.push(priorYearLastDay); return startStop; }; return this; })();