Copy codeThe Code is as follows :/**
2 * retrieve array elements (prototype extension or overload)
3 * @ param {o} The retrieved element value
4 * @ type int
5 * @ returns element index
6 */
7 Array. prototype. contains = function (o ){
8 var index =-1;
9 for (var I = 0; I <this. length; I ++) {if (this [I] = o) {index = I; break ;}}
Return index;
}
/**
* Date formatting (prototype extension or overloading)
* The format is YYYY/yyyy/YY, indicating the year.
* MM/M month
* W/w weeks
* Dd/DD/d/D Date
* Hh/HH/h/H time
* Mm/m minutes
* Ss/SS/s/S seconds
* @ Param {formatStr} format template
* @ Type string
* @ Returns date string
*/
Date. prototype. format = function (formatStr ){
Var str = formatStr;
Var Week = ['day', 'yi', '2', '3', '4', '5', '6'];
Str = str. replace (/yyyy | YYYY/, this. getFullYear ());
Str = str. replace (/yy | YY/, (this. getYear () % 100)> 9? (This. getYear () % 100). toString (): '0' + (this. getYear () % 100 ));
Str = str. replace (/MM/, (this. getMonth () + 1)> 9? (This. getMonth () + 1). toString (): '0' + (this. getMonth () + 1 ));
Str = str. replace (/M/g, this. getMonth ());
Str = str. replace (/w | W/g, Week [this. getDay ()]);
Str = str. replace (/dd | DD/, this. getDate ()> 9? This. getDate (). toString (): '0' + this. getDate ());
Str = str. replace (/d | D/g, this. getDate ());
Str = str. replace (/hh | HH/, this. getHours ()> 9? This. getHours (). toString (): '0' + this. getHours ());
Str = str. replace (/h | H/g, this. getHours ());
Str = str. replace (/mm/, this. getMinutes ()> 9? This. getMinutes (). toString (): '0' + this. getMinutes ());
Str = str. replace (/m/g, this. getMinutes ());
Str = str. replace (/ss | SS/, this. getSeconds ()> 9? This. getSeconds (). toString (): '0' + this. getSeconds ());
Str = str. replace (/s | S/g, this. getSeconds ());
Return str;
}
/**
* Compare date difference (prototype extension or overload)
* @ Param {strInterval} date type: 'y, m, d, h, n, s, W'
* @ Param {dtEnd} is a string in the date or valid date format.
* @ Type int
* @ Returns comparison result
*/
Date. prototype. dateDiff = function (strInterval, dtEnd ){
Var dtStart = this;
If (typeof dtEnd = 'string') {// if the string is converted to the date type
DtEnd = StringToDate (dtEnd );
}
Switch (strInterval ){
Case's ': return parseInt (dtEnd-dtStart)/1000 );
Case 'N': return parseInt (dtEnd-dtStart)/60000 );
Case 'H': return parseInt (dtEnd-dtStart)/3600000 );
Case 'D': return parseInt (dtEnd-dtStart)/86400000 );
Case 'W': return parseInt (dtEnd-dtStart)/(86400000*7 ));
Case 'M': return (dtEnd. getMonth () + 1) + (dtEnd. getFullYear ()-dtStart. getFullYear () * 12)-(dtStart. getMonth () + 1 );
Case 'y': return dtEnd. getFullYear ()-dtStart. getFullYear ();
}
}
/**
* Date calculation (prototype extension or heavy load)
* @ Param {strInterval} date type: 'y, m, d, h, n, s, W'
* @ Param {Number} quantity
* @ Type Date
* @ Returns: date after calculation
*/
Date. prototype. dateAdd = function (strInterval, Number ){
Var dtTmp = this;
Switch (strInterval ){
Case's ': return new Date (Date. parse (dtTmp) + (1000 * Number ));
Case 'N': return new Date (Date. parse (dtTmp) + (60000 * Number ));
Case 'H': return new Date (Date. parse (dtTmp) + (3600000 * Number ));
Case 'D': return new Date (Date. parse (dtTmp) + (86400000 * Number ));
Case 'W': return new Date (Date. parse (dtTmp) + (86400000*7) * Number ));
Case 'q': return new Date (dtTmp. getFullYear (), (dtTmp. getMonth () + Number * 3, dtTmp. getDate (), dtTmp. getHours (), dtTmp. getMinutes (), dtTmp. getSeconds ());
Case 'M': return new Date (dtTmp. getFullYear (), (dtTmp. getMonth () + Number, dtTmp. getDate (), dtTmp. getHours (), dtTmp. getMinutes (), dtTmp. getSeconds ());
Case 'y': return new Date (dtTmp. getFullYear () + Number), dtTmp. getMonth (), dtTmp. getDate (), dtTmp. getHours (), dtTmp. getMinutes (), dtTmp. getSeconds ());
}
}
/**
* Get date data information (prototype extension or heavy load)
* @ Param {interval} date type: 'y, m, d, h, n, s, W'
* @ Type int
* @ Returns specifies the date part.
*/
Date. prototype. datePart = function (interval ){
Var myDate = this;
Var partStr = '';
Var Week = ['day', 'yi', '2', '3', '4', '5', '6'];
Switch (interval)
{
Case 'y': partStr = myDate. getFullYear (); break;
Case 'M': partStr = myDate. getMonth () + 1; break;
Case 'D': partStr = myDate. getDate (); break;
Case 'W': partStr = Week [myDate. getDay ()]; break;
Case 'ww ': partStr = myDate. WeekNumOfYear (); break;
Case 'H': partStr = myDate. getHours (); break;
Case 'N': partStr = myDate. getMinutes (); break;
Case's ': partStr = myDate. getSeconds (); break;
}
Return partStr;
}
/**
* Divide dates into arrays (prototype extension or overload)
* @ Type array
* @ Returns date Array
*/
Date. prototype. toArray = function (){
Var myDate = this;
Var myArray = Array ();
MyArray [0] = myDate. getFullYear ();
MyArray [1] = myDate. getMonth () + 1;
MyArray [2] = myDate. getDate ();
MyArray [3] = myDate. getHours ();
MyArray [4] = myDate. getMinutes ();
MyArray [5] = myDate. getSeconds ();
Return myArray;
}
/**
* Get the number of days of the current month (prototype extension or overload)
* @ Type int
* @ Returns days
*/
Date. prototype. daysOfMonth = function (){
Var myDate = this;
Var ary = myDate. toArray ();
Var date1 = (new Date (ary [0], ary [1] + 1, 1 ));
Var date2 = date1.dateAdd ('M', 1 );
Var result = daysDiff (date1.format ('yyyy-MM-dd'), date2.format ('yyyy-MM-dd '));
Return result;
}
/**
* Determine a leap year (prototype extension or heavy load)
* @ Type boolean
* @ Returns whether it is a leap year true/false
*/
Date. prototype. isLeapYear = function (){
Return (0 = this. getYear () % 4 & (this. getYear () % 100! = 0) | (this. getYear () % 400 = 0 )));
}
/**
* Compare the number of days between two dates (custom)
* @ Param {DateOne} date 1
* @ Param {DateOne} date 2
* @ Type int
* @ Returns comparison result
*/
Function daysDiff (DateOne, DateTwo)
{
Var OneMonth = DateOne. substring (5, DateOne. lastIndexOf ('-'));
Var OneDay = DateOne. substring (DateOne. length, DateOne. lastIndexOf ('-') + 1 );
Var OneYear = DateOne. substring (0, DateOne. indexOf ('-'));
Var TwoMonth = DateTwo. substring (5, DateTwo. lastIndexOf ('-'));
Var TwoDay = DateTwo. substring (DateTwo. length, DateTwo. lastIndexOf ('-') + 1 );
Var TwoYear = DateTwo. substring (0, DateTwo. indexOf ('-'));
Var cha = (Date. parse (OneMonth + '/' + OneDay + '/' + OneYear)-Date. parse (TwoMonth + '/' + TwoDay + '/' + TwoYear)/86400000 );
Return Math. abs (cha );
}
/**
* Date calculation (custom)
* @ Param {strInterval} date type: 'y, m, d, h, n, s, W'
* @ Param {Number} quantity
* @ Param {prmDate} original date
* @ Type Date
* @ Returns: date after calculation
*/
Function dateAdd (interval, number, prmDate ){
Number = parseInt (number );
If (typeof (prmDate) = "string "){
PrmDate = prmDate. split (/\ D /);
-- PrmDate [1];
Eval ("var prmDate = new Date (" + prmDate. join (",") + ")");
}
If (typeof (prmDate) = "object "){
Var prmDate = prmDate
}
Switch (interval ){
Case "y": prmDate. setFullYear (prmDate. getFullYear () + number); break;
Case "m": prmDate. setMonth (prmDate. getMonth () + number); break;
Case "d": prmDate. setDate (prmDate. getDate () + number); break;
Case "w": prmDate. setDate (prmDate. getDate () + 7 * number); break;
Case "h": prmDate. setHours (prmDate. getHour () + number); break;
Case "n": prmDate. setMinutes (prmDate. getMinutes () + number); break;
Case "s": prmDate. setSeconds (prmDate. getSeconds () + number); break;
Case "l": prmDate. setMilliseconds (prmDate. getMilliseconds () + number); break;
}
Return prmDate;
}
/**
* Get the string length (prototype extension or overload)
* @ Type int
* @ Returns String Length
*/
String. prototype. len = function (){
Var arr = this. match (/[^ \ x00-\ xff]/ig );
Return this. length + (arr = null? 0: arr. length );
}
/**
* Take the specified number of characters from the left of the string (prototype extension or overload)
* @ Param {num} gets the number
* @ Type string
* @ Returns matched string
*/
String. prototype. left = function (num, mode ){
If (! /\ D +/. test (num) return (this );
Var str = this. substr (0, num );
If (! Mode) return str;
Var n = str. len ()-str. length;
Num = num-parseInt (n/2 );
Return this. substr (0, num );
}
/**
* Right-click the string and select a specified number of characters (prototype extension or overload)
* @ Param {num} gets the number
* @ Type string
* @ Returns matched string
*/
String. prototype. right = function (num, mode ){
If (! /\ D +/. test (num) return (this );
Var str = this. substr (this. length-num );
If (! Mode) return str;
Var n = str. len ()-str. length;
Num = num-parseInt (n/2 );
Return this. substr (this. length-num );
}
/**
* String inclusion (prototype extension or overload)
* @ Param {string: str} The substring to be searched
* @ Param {bool: mode} whether to ignore case sensitivity
* @ Type int
* @ Returns matched count
*/
String. prototype. matchCount = function (str, mode ){
Return eval ("this. match (/(" + str + ")/g" + (mode? "I": "") + "). length ");
}
/**
* Remove left and right spaces (prototype extension or overload)
* @ Type string
* @ Returns the processed string
*/
String. prototype. trim = function (){
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
/**
* Remove left spaces (prototype extension or overload)
* @ Type string
* @ Returns the processed string
*/
String. prototype. lTrim = function (){
Return this. replace (/(^ \ s *)/g ,"");
}
/**
* Remove right spaces (prototype extension or overload)
* @ Type string
* @ Returns the processed string
*/
String. prototype. rTrim = function (){
Return this. replace (/(\ s * $)/g ,"");
}
/**
* String Conversion to date type (prototype extension or overload)
* @ Type Date
* @ Returns date
*/
String. prototype. toDate = function (){
Var converted = Date. parse (this );
Var myDate = new Date (converted );
If (isNaN (myDate) {var arys = this. split ('-'); myDate = new Date (arys [0], -- arys [1], arys [2]);}
Return myDate;
}