Recently, some of the functions of the project are being reconstructed, and the front-end has basically overturned the original design. In the past six months, there have been new solutions. In the past few days, we have encountered some problems in front-end reconstruction and design. This module mainly controls the time and operates a large number of js Date objects. However, the Date method of js native is too small, which is too inconvenient to operate. Therefore, we intend to extend the prototype of Date.
I have been engaged in C # development for a long time, and C # affects my thinking. The DateTime operation in C # is very convenient, so it is referenced to extend the Date of js.Copy codeThe Code is as follows: // Add 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;
};
// 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;
};
// Add 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 copies per year to the value of this instance
Date. prototype. addYears = function (value ){
Var year = this. getFullYear ();
This. setFullYear (year + value );
Return this;
};
// Format the 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 don't want to say much about the usage, that is:Copy codeThe 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 you.