JS对日期封装使用,采用正则匹配日期结构进行显示日期。
/**
* 字符串转化为日期对象======》 调用格式为:str.format2Date("yyyy-MM-dd hh:mm:ss")
* @param {str} 传入特殊格式
* @return {Date}
* */
String.prototype.format2Date = function(style, option) {
if (style) {
let y = style.match("[Yy]{2,4}");
let M = style.match("M{1,2}");
let d = style.match("d{1,2}");
let h = style.match("h{1,2}");
let m = style.match("m{1,2}");
let s = style.match("s{1,2}");
let year = this.substring(y.index, y.index+y[0].length);
let month = this.substring(M.index, M.index+M[0].length);
let day = this.substring(d.index, d.index+d[0].length);
let hour = h != null?this.substring(h.index, h.index+h[0].length):'00';
let minute = m != null?this.substring(m.index, m.index+m[0].length):'00';
let second = s != null?this.substring(s.index, s.index+s[0].length):'00';
return new Date(year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second)
} else {
return new Date(this);
}
};
/**
* 时间日期格式化======》 调用格式为:date.format2String("yyyy-MM-dd hh:mm:ss")
* @param {[type]} style 时间格式字符串
* @returns {String}
*/
Date.prototype.format2String = function(style, option) {
var o = {
"[Yy]{2,4}" : this.getFullYear(), //year
"M{1,2}" : this.getMonth() + 1, //month
"d{1,2}" : this.getDate(), //day
"H{1,2}" : this.getHours(), //hour
"h{1,2}" : this.getHours()%24, //hour
"m{1,2}" : this.getMinutes(), //minute
"s{1,2}" : this.getSeconds(), //second
"[Ee]" : this.getDay(), //day in week
"[Ww]" : this.getDay(), //day in week
"[Qq]" : Math.floor((this.getMonth()+3)/3), //quarter
"S{3}|S{1}" : this.getMilliseconds()//millisecond
};
for(var k in o ){
style = style.replace(new RegExp("("+ k +")"), function(match, g, offset, string){
return ("0".repeat(match.length)+o[k]).substr( (""+o[k]).length );
})
}
return style;
};
/**
* 日期相减函数
* @param {date } 计算相减日期(传入日期无先后顺序)
*
* */
Date.prototype.dateSubtraction = function(date = new Date()) {
var start = Date.parse(new Date(this.replace(/-|\/|\,/g, "\/")));
var end = Date.parse(new Date(date.replace(/-|\/|\,/g, "\/")));
if (start > end) {
return parseInt((this.getTime() - date.getTime()) / (1000 * 60 * 60 * 24));
} else {
return parseInt((date.getTime() - this.getTime()) / (1000 * 60 * 60 * 24));
}
};
/**
* 年:加/减
* @param type 计算类型(1:相加,2:相减)
* @param {day} 相加或者相减天数
*
* */
Date.prototype.yearCompute = function(type, day = 1) {
var date = new Date(this);
switch (type) {
case 1: {
date.setFullYear(parseInt(this.getFullYear() + day));console.log("12313", date);
return date;
}
case 2: {
date.setFullYear(parseInt(this.getFullYear() - day));
return date;
}
}
};
/**
* 月:加/减
* @param type 计算类型(1:相加,2:相减)
* @param {month} 相加或者相减天数
*
* */
Date.prototype.monthCompute = function(type, month = 1) {
var date = new Date(this);
switch (type) {
case 1: {
date.setMonth(parseInt(this.getMonth() + month));
return date;
}
case 2: {
date.setMonth(parseInt(this.getMonth() - month));
return date;
}
}
};
/**
* 天:加/减
* @param type 计算类型(1:相加,2:相减)
* @param {day} 相加或者相减天数
*
* */
Date.prototype.dayCompute = function(type, day = 1) {
var date = new Date(this);
switch (type) {
case 1: {
date.setDate(parseInt(this.getDate() + day));
return date;
}
case 2: {
date.setDate(parseInt(this.getDate() - day));
return date;
}
}
};
/**
* 小时:加/减
* @param type 计算类型(1:相加,2:相减)
* @param {hour} 相加或者相减小时
*
* */
Date.prototype.hourCompute = function(type, hour = 1) {
var date = new Date(this);
switch (type) {
case 1: {
date.setHours(parseInt(this.getHours() + hour));
return date;
}
case 2: {
date.setHours(this.getHours() - hour);
return date;
}
}
};
/**
* 分钟:加/减
* @param type 计算类型(1:相加,2:相减)
* @param {minute} 相加或者相减小时
*
* */
Date.prototype.minuteCompute = function(type, minute = 1) {
var date = new Date(this);
switch (type) {
case 1: {
date.setMinutes(parseInt(this.getMinutes() + minute));
return date;
}
case 2: {
date.setMinutes(this.getMinutes() - minute);
return date;
}
}
};
/**
* 秒钟:加/减
* @param type 计算类型(1:相加,2:相减)
* @param {second} 相加或者相减小时
*
* */
Date.prototype.secondCompute = function(type, second = 1) {
var date = new Date(this);
switch (type) {
case 1: {
date.setSeconds(parseInt(this.getSeconds() + second));
return date;
}
case 2: {
date.setSeconds(parseInt(this.getSeconds() - second));
return date;
}
}
};
/**
* 毫秒:加/减
* @param type 计算类型(1:相加,2:相减)
* @param {millisecond} 相加或者相减小时
*
* */
Date.prototype.millisecondCompute = function(type, millisecond = 1) {
var date = new Date(this);
switch (type) {
case 1: {
date.setMilliseconds(parseInt(this.getMilliseconds() + millisecond));
return date;
}
case 2: {
date.setMilliseconds(parseInt(this.getMilliseconds() - millisecond));
return date;
}
}
};