_javascript techniques of JavaScript common function library based on prototype extension

Source: Internet
Author: User
Tags date1 eval getdate
Copy Code code as follows:

/**
2 * Retrieve array elements (prototype extension or overload)
3 * @param The value of the element retrieved by {o}
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 overload)
* Format YYYY/YYYY/YY/YY represents year
* mm/m Month
* W/W Week
* 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 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];
Str=str.replace (/yyyy| Yyyy/,this.getfullyear ());
Str=str.replace (/yy| yy/, (this.getyear ()%) >9? (This.getyear ()%). 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} format is a date-type or valid date format string
* @type int
* @returns Comparison results
*/
Date.prototype.dateDiff = function (Strinterval, dtend) {
var dtstart = this;
if (typeof dtend = = ' string ') {//if the string is converted to a 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 overload)
* @param {Strinterval} date type: ' Y, M, D, H, N, S, W '
* @param {Number} quantity
* @type Date
* @returns the 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 overload)
* @param {interval} date type: ' Y, M, D, H, N, S, W '
* @type int
* @returns the specified date part
*/
Date.prototype.datePart = function (interval) {
var mydate = this;
var partstr= ';
var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];
Switch (interval)
{
Case ' y ':p artstr = Mydate.getfullyear ();
Case ' m ':p artstr = Mydate.getmonth () +1;break;
Case ' d ':p artstr = Mydate.getdate ();
Case ' W ':p artstr = Week[mydate.getday ()];break;
Case ' ww ':p artstr = Mydate.weeknumofyear ();
Case ' h ':p artstr = mydate.gethours ();
Case ' n ':p artstr = Mydate.getminutes ();
Case ' s ':p artstr = Mydate.getseconds ();
}
return partstr;
}

/**
* Split date into groups (prototype extension or overload)
* @type Array
* @returns array of dates
*/
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;
}

/**
* Judging leap years (prototype extension or overload)
* @type Boolean
* @returns 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 difference two dates (custom)
* @param {Dateone} date One
* @param {Dateone} date two
* @type int
* @returns Comparison results
*/
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 the 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 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);
}

/**
* String takes a specified number of characters (prototype extension or overload)
* @param {num} get number
* @type String
* @returns Matching 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);
}

/**
* String right takes a specified number of characters (prototype extension or overload)
* @param {num} get number
* @type String
* @returns Matching 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 contains (prototype extension or overload)
* @param {STRING:STR} substring to search for
* @param {Bool:mode} ignores case
* @type int
* Number of @returns matches
*/
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 space (prototype extension or overload)
* @type String
* @returns the processed string
*/
String.prototype.lTrim = function () {
Return This.replace (/(^\s*)/g, "");
}

/**
* Remove right space (prototype extension or overload)
* @type String
* @returns the processed string
*/
String.prototype.rTrim = function () {
Return This.replace (/(\s*$)/g, "");
}

/**
* String 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;
}

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.