The date method of extending JavaScript implements code (prototype) _javascript tips

Source: Internet
Author: User
Tags getdate
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.