Home > Web Front-end > JS Tutorial > Extended JavaScript Date method implementation code (prototype)_javascript skills

Extended JavaScript Date method implementation code (prototype)_javascript skills

WBOY
Release: 2016-05-16 18:15:48
Original
1136 people have browsed it

Recently, some functions of the project are being reconstructed, and the front-end has basically overturned the original design, and there are new solutions based on the accumulation in the previous six months. I have been refactoring and designing the front end these days and have encountered some problems. Because the main purpose of this module is to control time, it operates a large number of js Date objects, but there are too few native js Date methods, which is too inconvenient to operate. So I plan to extend the prototype of Date.

I have been engaged in C# development for a long time, and C# has influenced my thinking. The operation of DateTime in C# is very convenient, so I used it to extend the Date of js.

Copy code The code is as follows:

//Add the specified number of milliseconds to this instance Value on
Date.prototype.addMilliseconds = function (value) {
var millisecond = this.getMilliseconds();
this.setMilliseconds(millisecond value);
return this;
};
//Add 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;
};
//Add 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;
};
//Add 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;
};
//Add the specified number of weeks to the value of this instance
Date.prototype.addWeeks = function (value) {
return this.addDays(value * 7);
};
//Add 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;
};
//Add 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;
};
//Format date display 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;
}

I think there is no need to say more about how to use it, it is:
Copy code The code is as follows:

var date = new Date();
date.addHours(1);
date .addYears(2);
document.write(date.format('yyyy-MM-dd hh:mm:ss'));

I hope this extension method can help everyone.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template