JS Date时间函数
1.基础时间函数:Link:设置日期 | Link:获取日期
2.天/周:今天/昨天/近三天/近七天/本周
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var DateUtil = new function() { /** * 获取:今天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ this.getCurrentDays = function(date, type = 0) { date = date || new Date(); return date.getRangeDate(0, 0); }; /** * 获取:昨天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ this.getYesterdays = function(date, type = 0) { date = date || new Date(); return date.getRangeDate(-1, -1); }; /** * 获取:近三天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ this.getNearlyThreeDays = function(date, type = 0) { date = date || new Date(); return date.getRangeDate(-2, 0); }; /** * 获取:近七天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ this.getNearlySevenDays = function(date, type = 0) { date = date || new Date(); return date.getRangeDate(-6, 0); }; /** * 获取:本周 * seed: 种子,默认是0,代表每周从礼拜天开始 */ this.getCurrentWeek = function(date, seed = 0) { date = date || new Date(); // 以数值获取周名(0-6) const weekIndex = date.getDay(); const startIndex = seed - weekIndex; const endIndex = seed + 6 - weekIndex; return date.getRangeDate(startIndex, endIndex); }; /** * 获取时间区间 * startDay: 距离当前日期多少天,负数为历史日期 * endDay: 距离当前日期多少天,负数为历史日期 */ this.getRangeDate = function(date, startDay, endDay) { date = date || new Date(); return date.getRangeDate(startDay, endDay); }; return this; }();
3.月/旬:上旬/中旬/下旬/本月
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var DateUtil = new function() { /** * 获取:上旬 */ this.getEarlyDays = function(date) { date = date || new Date(); var startDate = date.setDate(1); var endDate = date.setDate(10); return new Array(new Date(startDate).resetMinTime(), new Date(endDate).resetMinTime()); }; /** * 获取:中旬 */ this.getMidDays = function(date) { date = date || new Date(); var startDate = date.setDate(11); var endDate = date.setDate(20); return new Array(new Date(startDate).resetMinTime(), new Date(endDate).resetMinTime()); }; /** * 获取:下旬 */ this.getLatesDays = function(date) { date = date || new Date(); var startDate = date.setDate(21); var endDate = date.getMonthMaxDay(); return new Array(new Date(startDate).resetMinTime(), new Date(endDate).resetMinTime()); }; /** * 获取:本月 */ this.getMonths = function(date) { date = date || new Date(); var startDate = date.getMonthMinDay(); var endDate = date.getMonthMaxDay(); return new Array(new Date(startDate).resetMinTime(), new Date(endDate).resetMinTime()); }; }();
4.月/季:第一季度/第二季度/第三季度/第四季度/今年
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var DateUtil = new function() { /** * 获取:第一季度 */ this.getFirstQuarterDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = new Date(currentYear+'-01-01').resetMinTime(); var endDate = new Date(currentYear+'-04-01').getYesterday().resetMaxTime(); return new Array(startDate, endDate); }; /** * 获取:第二季度 */ this.getSecondQuarterDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = new Date(currentYear+'-04-01').resetMinTime(); var endDate = new Date(currentYear+'-07-01').getYesterday().resetMaxTime(); return new Array(startDate, endDate); }; /** * 获取:第三季度 */ this.getThirdQuarterDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = new Date(currentYear+'-07-01').resetMinTime(); var endDate = new Date(currentYear+'-10-01').getYesterday().resetMaxTime(); return new Array(startDate, endDate); }; /** * 获取:第四季度 */ this.getFourthQuarterDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = new Date(currentYear+'-10-01').resetMinTime(); var endDate = date.getYearMaxDay(); return new Array(startDate, endDate); }; /** * 获取:今年 */ this.getYearDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = date.getYearMinDay(); var endDate = date.getYearMaxDay(); return new Array(startDate, endDate); }; return this; }();
5.拓展函数:Link
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 获取当前月最小天数 */ Date.prototype.getMonthMinDay = function(date) { date = arguments[0] || new Date(); date.setDate(1); return date.resetMinTime(); } /** * 获取当前月最大天数 */ Date.prototype.getMonthMaxDay = function(date) { date = arguments[0] || new Date(); date.setDate(1); date.setMonth(date.getMonth() + 1); return date.addDay(-1, 1); } /** * 获取当前年最小天数 */ Date.prototype.getYearMinDay = function(date) { date = arguments[0] || new Date(); date.setDate(1); date.setMonth(0); // 获取月(0-11) return date.resetMinTime(); } /** * 获取当前年最大天数 */ Date.prototype.getYearMaxDay = function(date) { date = arguments[0] || new Date(); date.setDate(1); date.setMonth(0); // 获取月(0-11) date.setFullYear(date.getFullYear() + 1); return date.addDay(-1, 1); } /** * 获取昨天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ Date.prototype.getYesterday = function(type) { const yesterdayTime = this.addDay(-1, type); return yesterdayTime; } /** * 获取时间区间 * startDay: 距离当前日期多少天,负数为历史日期 * endDay: 距离当前日期多少天,负数为历史日期 */ Date.prototype.getRangeDate = function(startDay, endDay) { const currentDate = this; var startDate = currentDate.addDay(startDay, 1); var endDate = currentDate.addDay(endDay, 2); return new Array(startDate, endDate); } /** * 获取时间区间 * dayNum: 增加天数,负数为历史日期 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ Date.prototype.addDay = function(dayNum, type = 0) { let currentDate = this; if(type == 1) currentDate = this.resetMinTime(); else if(type == 2) currentDate = this.resetMaxTime(); else {} const newTime = currentDate.getTime() + (24 * 60 * 60 * 1000 * dayNum) return new Date(newTime); } /** * 设置时分秒 */ Date.prototype.setTime = function(hours, minutes, seconds) { let myDate = this; myDate.setHours(hours); myDate.setMinutes(minutes); myDate.setSeconds(seconds); myDate.setMilliseconds(0); return myDate; } /** * 重置时分秒[00:00:00] */ Date.prototype.resetMinTime = function() { return this.setTime(0, 0, 0); } /** * 重置时分秒[23:59:59] */ Date.prototype.resetMaxTime = function() { return this.setTime(23, 59, 59); } /** * 把日期分割成数组 [年、月、日、时、分、秒] */ Date.prototype.toArray = function() { var myArray = Array(); myArray[0] = this.getFullYear(); myArray[1] = this.getMonth() + 1; // 获取月(0-11) myArray[2] = this.getDate(); myArray[3] = this.getHours(); myArray[4] = this.getMinutes(); myArray[5] = this.getSeconds(); return myArray; }
6.demo
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 获取当前月最小天数 */ Date.prototype.getMonthMinDay = function(date) { date = arguments[0] || new Date(); date.setDate(1); return date.resetMinTime(); } /** * 获取当前月最大天数 */ Date.prototype.getMonthMaxDay = function(date) { date = arguments[0] || new Date(); date.setDate(1); date.setMonth(date.getMonth() + 1); return date.addDay(-1, 1); } /** * 获取当前年最小天数 */ Date.prototype.getYearMinDay = function(date) { date = arguments[0] || new Date(); date.setDate(1); date.setMonth(0); // 获取月(0-11) return date.resetMinTime(); } /** * 获取当前年最大天数 */ Date.prototype.getYearMaxDay = function(date) { date = arguments[0] || new Date(); date.setDate(1); date.setMonth(0); // 获取月(0-11) date.setFullYear(date.getFullYear() + 1); return date.addDay(-1, 1); } /** * 获取昨天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ Date.prototype.getYesterday = function(type) { const yesterdayTime = this.addDay(-1, type); return yesterdayTime; } /** * 获取时间区间 * startDay: 距离当前日期多少天,负数为历史日期 * endDay: 距离当前日期多少天,负数为历史日期 */ Date.prototype.getRangeDate = function(startDay, endDay) { const currentDate = this; var startDate = currentDate.addDay(startDay, 1); var endDate = currentDate.addDay(endDay, 2); return new Array(startDate, endDate); } /** * 获取时间区间 * dayNum: 增加天数,负数为历史日期 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ Date.prototype.addDay = function(dayNum, type = 0) { let currentDate = this; if(type == 1) currentDate = this.resetMinTime(); else if(type == 2) currentDate = this.resetMaxTime(); else {} const newTime = currentDate.getTime() + (24 * 60 * 60 * 1000 * dayNum) return new Date(newTime); } /** * 设置时分秒 */ Date.prototype.setTime = function(hours, minutes, seconds) { let myDate = this; myDate.setHours(hours); myDate.setMinutes(minutes); myDate.setSeconds(seconds); myDate.setMilliseconds(0); return myDate; } /** * 重置时分秒[00:00:00] */ Date.prototype.resetMinTime = function() { return this.setTime(0, 0, 0); } /** * 重置时分秒[23:59:59] */ Date.prototype.resetMaxTime = function() { return this.setTime(23, 59, 59); } /** * 把日期分割成数组 [年、月、日、时、分、秒] */ Date.prototype.toArray = function() { var myArray = Array(); myArray[0] = this.getFullYear(); myArray[1] = this.getMonth() + 1; // 获取月(0-11) myArray[2] = this.getDate(); myArray[3] = this.getHours(); myArray[4] = this.getMinutes(); myArray[5] = this.getSeconds(); return myArray; } var DateUtil = new function() { /** * 获取:今天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ this.getCurrentDays = function(date, type = 0) { date = date || new Date(); return date.getRangeDate(0, 0); }; /** * 获取:昨天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ this.getYesterdays = function(date, type = 0) { date = date || new Date(); return date.getRangeDate(-1, -1); }; /** * 获取:近三天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ this.getNearlyThreeDays = function(date, type = 0) { date = date || new Date(); return date.getRangeDate(-2, 0); }; /** * 获取:近七天 * type: 0-不修改时间;1-重置为最小时间;2-重置为最大时间 */ this.getNearlySevenDays = function(date, type = 0) { date = date || new Date(); return date.getRangeDate(-6, 0); }; /** * 获取:本周 * seed: 种子,默认是0,代表每周从礼拜天开始 */ this.getCurrentWeek = function(date, seed = 0) { date = date || new Date(); // 以数值获取周名(0-6) const weekIndex = date.getDay(); const startIndex = seed - weekIndex; const endIndex = seed + 6 - weekIndex; return date.getRangeDate(startIndex, endIndex); }; /** * 获取:上旬 */ this.getEarlyDays = function(date) { date = date || new Date(); var startDate = date.setDate(1); var endDate = date.setDate(10); return new Array(new Date(startDate).resetMinTime(), new Date(endDate).resetMinTime()); }; /** * 获取:中旬 */ this.getMidDays = function(date) { date = date || new Date(); var startDate = date.setDate(11); var endDate = date.setDate(20); return new Array(new Date(startDate).resetMinTime(), new Date(endDate).resetMinTime()); }; /** * 获取:下旬 */ this.getLatesDays = function(date) { date = date || new Date(); var startDate = date.setDate(21); var endDate = date.getMonthMaxDay(); return new Array(new Date(startDate).resetMinTime(), new Date(endDate).resetMinTime()); }; /** * 获取:本月 */ this.getMonths = function(date) { date = date || new Date(); var startDate = date.getMonthMinDay(); var endDate = date.getMonthMaxDay(); return new Array(new Date(startDate).resetMinTime(), new Date(endDate).resetMinTime()); }; /** * 获取:第一季度 */ this.getFirstQuarterDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = new Date(currentYear+'-01-01').resetMinTime(); var endDate = new Date(currentYear+'-04-01').getYesterday().resetMaxTime(); return new Array(startDate, endDate); }; /** * 获取:第二季度 */ this.getSecondQuarterDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = new Date(currentYear+'-04-01').resetMinTime(); var endDate = new Date(currentYear+'-07-01').getYesterday().resetMaxTime(); return new Array(startDate, endDate); }; /** * 获取:第三季度 */ this.getThirdQuarterDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = new Date(currentYear+'-07-01').resetMinTime(); var endDate = new Date(currentYear+'-10-01').getYesterday().resetMaxTime(); return new Array(startDate, endDate); }; /** * 获取:第四季度 */ this.getFourthQuarterDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = new Date(currentYear+'-10-01').resetMinTime(); var endDate = date.getYearMaxDay(); return new Array(startDate, endDate); }; /** * 获取:今年 */ this.getYearDays = function(date) { date = date || new Date(); const currentYear = date.getFullYear(); var startDate = date.getYearMinDay(); var endDate = date.getYearMaxDay(); return new Array(startDate, endDate); }; return this; }();