JS 日期封裝操作

2021-10-25 03:08:19 字數 3211 閱讀 8505

js對日期封裝使用,採用正則匹配日期結構進行顯示日期。

/**

* 字串轉化為日期物件*****=》 呼叫格式為:str.format2date("yyyy-mm-dd hh:mm:ss")

* @param 傳入特殊格式

* @return

* */

string.prototype.format2date = function(style, option) ");

let m = style.match("m");

let d = style.match("d");

let h = style.match("h");

let m = style.match("m");

let s = style.match("s");

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

};/**

* 時間日期格式化*****=》 呼叫格式為:date.format2string("yyyy-mm-dd hh:mm:ss")

* @param style 時間格式字串

* @returns

*/date.prototype.format2string = function(style, option) " : this.getfullyear(), //year

"m" : this.getmonth() + 1, //month

"d" : this.getdate(), //day

"h" : this.gethours(), //hour

"h" : this.gethours()%24, //hour

"m" : this.getminutes(), //minute

"s" : this.getseconds(), //second

"[ee]" : this.getday(), //day in week

"[ww]" : this.getday(), //day in week

"[qq]" : math.floor((this.getmonth()+3)/3), //quarter

"s|s" : this.getmilliseconds()//millisecond

};for(var k in o ))

}return style;

};/**

* 日期相減函式

* @param 計算相減日期(傳入日期無先後順序)

* * */

date.prototype.datesubtraction = function(date = new date()) else

};/**

* 年:加/減

* @param type 計算型別(1:相加,2:相減)

* @param 相加或者相減天數

* * */

date.prototype.yearcompute = function(type, day = 1)

case 2:

}};/**

* 月:加/減

* @param type 計算型別(1:相加,2:相減)

* @param 相加或者相減天數

* * */

date.prototype.monthcompute = function(type, month = 1)

case 2:

}};/**

* 天:加/減

* @param type 計算型別(1:相加,2:相減)

* @param 相加或者相減天數

* * */

date.prototype.daycompute = function(type, day = 1)

case 2:

}};/**

* 小時:加/減

* @param type 計算型別(1:相加,2:相減)

* @param 相加或者相減小時

* * */

date.prototype.hourcompute = function(type, hour = 1)

case 2:

}};/**

* 分鐘:加/減

* @param type 計算型別(1:相加,2:相減)

* @param 相加或者相減小時

* * */

date.prototype.minutecompute = function(type, minute = 1)

case 2:

}};/**

* 秒鐘:加/減

* @param type 計算型別(1:相加,2:相減)

* @param 相加或者相減小時

* * */

date.prototype.secondcompute = function(type, second = 1)

case 2:

}};/**

* 毫秒:加/減

* @param type 計算型別(1:相加,2:相減)

* @param 相加或者相減小時

* * */

date.prototype.millisecondcompute = function(type, millisecond = 1)

case 2:

}};

js常用日期函式封裝

var obj return false 將日期格式化輸出 2019 08 25 format function date,s 輸入08 25型別的日期 formatmonday function date,s 判段月份的天數 getdays function year,month return 2...

js日期操作

目前主流的js日期操作方法是,將日期物件轉化成毫秒數進行運算。例如 日期2017 10 10,需要知道7天之後日期是多少,可以如下操作 var today new date 2017 10 10 days 7,year,month,day,afterweektimes,afterweek,res 目...

JS日期操作

日期,在原有日期基礎上,增加days天數,預設增加1天 function adddate date,days var date new date date date.setdate date.getdate days var month date.getmonth 1 var day date.ge...