Recently, some of the features of the project are being reconstructed, the front-end has basically overturned the original design, in the previous half of the accumulation of a new program. These days in the front of the refactoring and design, encountered some problems. Because this module is mainly the control of time, a large number of Operations JS Date object, but JS native date method is too little, too inconvenient to operate. So I intend to extend the prototype of date.
Long engaged in the development of C #, is affected by C # my thinking. The operation of DateTime in C # is very convenient, so reference it to the JS date did an extension.
Copy Code code as follows:
Adds the specified number of milliseconds to the value of this instance
Date.prototype.addMilliseconds = function (value) {
var millisecond = This.getmilliseconds ();
This.setmilliseconds (millisecond + value);
return this;
};
Adds the specified number of seconds to the value of this instance
Date.prototype.addSeconds = function (value) {
var second = This.getseconds ();
This.setseconds (second + value);
return this;
};
Adds the specified number of minutes to the value of this instance
Date.prototype.addMinutes = function (value) {
var minute = This.addminutes ();
This.setminutes (minute + value);
return this;
};
Adds the specified number of hours to the value of this instance
Date.prototype.addHours = function (value) {
var hour = this.gethours ();
This.sethours (hour + value);
return this;
};
Adds the specified number of days to the value of this instance
Date.prototype.addDays = function (value) {
var date = This.getdate ();
This.setdate (date + value);
return this;
};
Adds the specified number of weeks to the value of this instance
Date.prototype.addWeeks = function (value) {
return This.adddays (value * 7);
};
Adds the specified number of months to the value of this instance
Date.prototype.addMonths = function (value) {
var month = This.getmonth ();
This.setmonth (month + value);
return this;
};
Adds the specified number of years to the value of this instance
Date.prototype.addYears = function (value) {
var year = This.getfullyear ();
This.setfullyear (year + value);
return this;
};
The format date shows 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]: ("+ o[k]"). substr (("" + o[k]). length);
}
}
return format;
}
How to use the method I think I should not say more, that is:
Copy Code code as follows:
var date = new Date ();
Date.addhours (1);
Date.addyears (2);
document.write (Date.format (' Yyyy-mm-dd hh:mm:ss '));
Hopefully this extension will help everyone.