擴展javascript的Date方法實現(xiàn)代碼(prototype)
更新時間:2010年11月20日 20:24:58 作者:
長期從事C#的開發(fā),被C#影響著我的思維。C#中DateTime的操作就很方便,于是就參考它對js的Date做了擴展。
最近項目的部分功能正在重構(gòu),前端也基本上推翻了原來的設(shè)計,在之前半年的積累上有了新的方案。這幾天在做前端的重構(gòu)和設(shè)計,遇到了一些問題。因為這個模塊最主要的還是對時間的控制,大量的操作js的Date對象,可是js原生的Date方法太少了,操作起來太不方便。于是打算擴展下Date的prototype。
長期從事C#的開發(fā),被C#影響著我的思維。C#中DateTime的操作就很方便,于是就參考它對js的Date做了擴展。
//將指定的毫秒數(shù)加到此實例的值上
Date.prototype.addMilliseconds = function (value) {
var millisecond = this.getMilliseconds();
this.setMilliseconds(millisecond + value);
return this;
};
//將指定的秒數(shù)加到此實例的值上
Date.prototype.addSeconds = function (value) {
var second = this.getSeconds();
this.setSeconds(second + value);
return this;
};
//將指定的分鐘數(shù)加到此實例的值上
Date.prototype.addMinutes = function (value) {
var minute = this.addMinutes();
this.setMinutes(minute + value);
return this;
};
//將指定的小時數(shù)加到此實例的值上
Date.prototype.addHours = function (value) {
var hour = this.getHours();
this.setHours(hour + value);
return this;
};
//將指定的天數(shù)加到此實例的值上
Date.prototype.addDays = function (value) {
var date = this.getDate();
this.setDate(date + value);
return this;
};
//將指定的星期數(shù)加到此實例的值上
Date.prototype.addWeeks = function (value) {
return this.addDays(value * 7);
};
//將指定的月份數(shù)加到此實例的值上
Date.prototype.addMonths = function (value) {
var month = this.getMonth();
this.setMonth(month + value);
return this;
};
//將指定的年份數(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)該不用多說了,就是:
var date = new Date();
date.addHours(1);
date.addYears(2);
document.write(date.format('yyyy-MM-dd hh:mm:ss'));
希望這個擴展方法可以幫助到大家。
長期從事C#的開發(fā),被C#影響著我的思維。C#中DateTime的操作就很方便,于是就參考它對js的Date做了擴展。
復(fù)制代碼 代碼如下:
//將指定的毫秒數(shù)加到此實例的值上
Date.prototype.addMilliseconds = function (value) {
var millisecond = this.getMilliseconds();
this.setMilliseconds(millisecond + value);
return this;
};
//將指定的秒數(shù)加到此實例的值上
Date.prototype.addSeconds = function (value) {
var second = this.getSeconds();
this.setSeconds(second + value);
return this;
};
//將指定的分鐘數(shù)加到此實例的值上
Date.prototype.addMinutes = function (value) {
var minute = this.addMinutes();
this.setMinutes(minute + value);
return this;
};
//將指定的小時數(shù)加到此實例的值上
Date.prototype.addHours = function (value) {
var hour = this.getHours();
this.setHours(hour + value);
return this;
};
//將指定的天數(shù)加到此實例的值上
Date.prototype.addDays = function (value) {
var date = this.getDate();
this.setDate(date + value);
return this;
};
//將指定的星期數(shù)加到此實例的值上
Date.prototype.addWeeks = function (value) {
return this.addDays(value * 7);
};
//將指定的月份數(shù)加到此實例的值上
Date.prototype.addMonths = function (value) {
var month = this.getMonth();
this.setMonth(month + value);
return this;
};
//將指定的年份數(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)該不用多說了,就是:
復(fù)制代碼 代碼如下:
var date = new Date();
date.addHours(1);
date.addYears(2);
document.write(date.format('yyyy-MM-dd hh:mm:ss'));
希望這個擴展方法可以幫助到大家。
您可能感興趣的文章:
- JavaScript 模仿vbs中的 DateAdd() 函數(shù)的代碼
- Prototype Date對象 學(xué)習(xí)
- php Smarty date_format [格式化時間日期]
- js實現(xiàn)的日期操作類DateTime函數(shù)代碼
- Mysql 日期時間 DATE_FORMAT(date,format)
- JavaScript Date對象 日期獲取函數(shù)
- JS Date函數(shù)整理方便使用
- JS中Date日期函數(shù)中的參數(shù)使用介紹
- fmt:formatDate的輸出格式詳解
- js用Date對象的setDate()函數(shù)對日期進行加減操作
- JavaScript下的時間格式處理函數(shù)Date.prototype.format
相關(guān)文章
JavaScript中常見的高階函數(shù)總結(jié)
JavaScript的函數(shù)其實都指向某個變量,既然變量可以指向函數(shù),函數(shù)的參數(shù)能接收變量,那么一個函數(shù)就可以接收另一個函數(shù)作為參數(shù),這種函數(shù)就稱之為高階函數(shù),這篇文章主要給大家介紹了關(guān)于JavaScript中常見的高階函數(shù),需要的朋友可以參考下2022-02-02
bootstrap中模態(tài)框、模態(tài)框的屬性實例詳解
這篇文章主要介紹了bootstrap中模態(tài)框、模態(tài)框的屬性實例詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
JavaScript?雙位非運算(~~?操作符)使用場景實例探索
本文為大家介紹JavaScript中雙位非運算?~~,?~~?操作符是一個強大且經(jīng)常被忽視的特性,它提供了一種快速、簡潔的方式來處理數(shù)字和執(zhí)行類型轉(zhuǎn)換,通??梢员挥糜跀?shù)學(xué)計算和類型轉(zhuǎn)換,我們先了解一下?~~?的基本概念和它的一些應(yīng)用場景2024-01-01
關(guān)于JS通過google翻譯插件實現(xiàn)多語言版本
這篇文章主要介紹了JS通過google翻譯插件實現(xiàn)多語言版本,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
簡單實用的js調(diào)試logger組件實現(xiàn)代碼
開發(fā)js組件的時間調(diào)試總是麻煩的,最常用的就是用alert或者debugger來測試js的運行狀態(tài)。2010-11-11

