擴(kuò)展javascript的Date方法實(shí)現(xiàn)代碼(prototype)
更新時(shí)間:2010年11月20日 20:24:58 作者:
長(zhǎng)期從事C#的開(kāi)發(fā),被C#影響著我的思維。C#中DateTime的操作就很方便,于是就參考它對(duì)js的Date做了擴(kuò)展。
最近項(xiàng)目的部分功能正在重構(gòu),前端也基本上推翻了原來(lái)的設(shè)計(jì),在之前半年的積累上有了新的方案。這幾天在做前端的重構(gòu)和設(shè)計(jì),遇到了一些問(wèn)題。因?yàn)檫@個(gè)模塊最主要的還是對(duì)時(shí)間的控制,大量的操作js的Date對(duì)象,可是js原生的Date方法太少了,操作起來(lái)太不方便。于是打算擴(kuò)展下Date的prototype。
長(zhǎng)期從事C#的開(kāi)發(fā),被C#影響著我的思維。C#中DateTime的操作就很方便,于是就參考它對(duì)js的Date做了擴(kuò)展。
//將指定的毫秒數(shù)加到此實(shí)例的值上
Date.prototype.addMilliseconds = function (value) {
var millisecond = this.getMilliseconds();
this.setMilliseconds(millisecond + value);
return this;
};
//將指定的秒數(shù)加到此實(shí)例的值上
Date.prototype.addSeconds = function (value) {
var second = this.getSeconds();
this.setSeconds(second + value);
return this;
};
//將指定的分鐘數(shù)加到此實(shí)例的值上
Date.prototype.addMinutes = function (value) {
var minute = this.addMinutes();
this.setMinutes(minute + value);
return this;
};
//將指定的小時(shí)數(shù)加到此實(shí)例的值上
Date.prototype.addHours = function (value) {
var hour = this.getHours();
this.setHours(hour + value);
return this;
};
//將指定的天數(shù)加到此實(shí)例的值上
Date.prototype.addDays = function (value) {
var date = this.getDate();
this.setDate(date + value);
return this;
};
//將指定的星期數(shù)加到此實(shí)例的值上
Date.prototype.addWeeks = function (value) {
return this.addDays(value * 7);
};
//將指定的月份數(shù)加到此實(shí)例的值上
Date.prototype.addMonths = function (value) {
var month = this.getMonth();
this.setMonth(month + value);
return this;
};
//將指定的年份數(shù)加到此實(shí)例的值上
Date.prototype.addYears = function (value) {
var year = this.getFullYear();
this.setFullYear(year + value);
return this;
};
//格式化日期顯示 format="yyyy-MM-dd hh:mm:ss";
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
使用方法我想應(yīng)該不用多說(shuō)了,就是:
var date = new Date();
date.addHours(1);
date.addYears(2);
document.write(date.format('yyyy-MM-dd hh:mm:ss'));
希望這個(gè)擴(kuò)展方法可以幫助到大家。
長(zhǎng)期從事C#的開(kāi)發(fā),被C#影響著我的思維。C#中DateTime的操作就很方便,于是就參考它對(duì)js的Date做了擴(kuò)展。
復(fù)制代碼 代碼如下:
//將指定的毫秒數(shù)加到此實(shí)例的值上
Date.prototype.addMilliseconds = function (value) {
var millisecond = this.getMilliseconds();
this.setMilliseconds(millisecond + value);
return this;
};
//將指定的秒數(shù)加到此實(shí)例的值上
Date.prototype.addSeconds = function (value) {
var second = this.getSeconds();
this.setSeconds(second + value);
return this;
};
//將指定的分鐘數(shù)加到此實(shí)例的值上
Date.prototype.addMinutes = function (value) {
var minute = this.addMinutes();
this.setMinutes(minute + value);
return this;
};
//將指定的小時(shí)數(shù)加到此實(shí)例的值上
Date.prototype.addHours = function (value) {
var hour = this.getHours();
this.setHours(hour + value);
return this;
};
//將指定的天數(shù)加到此實(shí)例的值上
Date.prototype.addDays = function (value) {
var date = this.getDate();
this.setDate(date + value);
return this;
};
//將指定的星期數(shù)加到此實(shí)例的值上
Date.prototype.addWeeks = function (value) {
return this.addDays(value * 7);
};
//將指定的月份數(shù)加到此實(shí)例的值上
Date.prototype.addMonths = function (value) {
var month = this.getMonth();
this.setMonth(month + value);
return this;
};
//將指定的年份數(shù)加到此實(shí)例的值上
Date.prototype.addYears = function (value) {
var year = this.getFullYear();
this.setFullYear(year + value);
return this;
};
//格式化日期顯示 format="yyyy-MM-dd hh:mm:ss";
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
使用方法我想應(yīng)該不用多說(shuō)了,就是:
復(fù)制代碼 代碼如下:
var date = new Date();
date.addHours(1);
date.addYears(2);
document.write(date.format('yyyy-MM-dd hh:mm:ss'));
希望這個(gè)擴(kuò)展方法可以幫助到大家。
您可能感興趣的文章:
- JavaScript 模仿vbs中的 DateAdd() 函數(shù)的代碼
- Prototype Date對(duì)象 學(xué)習(xí)
- php Smarty date_format [格式化時(shí)間日期]
- js實(shí)現(xiàn)的日期操作類(lèi)DateTime函數(shù)代碼
- Mysql 日期時(shí)間 DATE_FORMAT(date,format)
- JavaScript Date對(duì)象 日期獲取函數(shù)
- JS Date函數(shù)整理方便使用
- JS中Date日期函數(shù)中的參數(shù)使用介紹
- fmt:formatDate的輸出格式詳解
- js用Date對(duì)象的setDate()函數(shù)對(duì)日期進(jìn)行加減操作
- JavaScript下的時(shí)間格式處理函數(shù)Date.prototype.format
相關(guān)文章
javascript和php使用ajax通信傳遞JSON的實(shí)例
今天小編就為大家分享一篇javascript和php使用ajax通信傳遞JSON的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Postman如何實(shí)現(xiàn)參數(shù)化執(zhí)行及斷言處理
這篇文章主要介紹了Postman如何實(shí)現(xiàn)參數(shù)化執(zhí)行及斷言處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07基于Three.js實(shí)現(xiàn)360度全景圖片
這篇文章主要為大家詳細(xì)介紹了基于Three.js實(shí)現(xiàn)360度全景圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

JS 實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼的“59秒后重新發(fā)送驗(yàn)證短信”功能
這篇文章主要介紹了JS 實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼的“59秒后重新發(fā)送驗(yàn)證短信”功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
2019-08-08