Some JS tool classes
Copy Code/** * Created by sevennight on 15-1-31. * JS Common Tool Class *//** * method function: "Format Time" * Use method * Example: * Use one: * var now = new Date (); * var nowstr = Now.dateformat ("Yyyy-mm-dd hh:mm:ss"); * Use mode two: * new Date (). DateFormat ("yyyy mm month DD day"); * New Date (). DateFormat ("mm/dd/yyyy"); * New Date (). DateFormat ("YyyyMMdd"); * New Date (). DateFormat ("Yyyy-mm-dd hh:mm:ss"); * @param format {Date} passed in the date type to be formatted * @returns {2015-01-31 16:30:00} */date.prototype.dateformat = function (format) {VA R 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.getmon Th () +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]: (" XX "+ o[k]). subst R (("" + o[k]). length); }} return format;} /*********************************************************************** * Date Time tool class * * Note: Call mode, Deteutil. Method Name * * ****************************************** /var dateutil = {/* * * Method function: "Take the date of the day is days" * Use method: Dateutil.nowfewweeks (new Date ()); * @param date{date} incoming Date type * @returns {Thursday, ...} */nowfewweeks:function (date) {if (date instanceof date) {var dayNames = new Array ("Sunday", "Monday", "Tuesday", " Wednesday "," Thursday "," Friday "," Saturday "); Return Daynames[date.getday ()]; } else{return "Param error,date type!"; }},/* * Method Function: "String conversion to Date" * Method of Use: Dateutil.strturndate ("2010-01-01"); * @param str {string} date in string format, incoming format: YYYY-MM-DD (2015-01-31) * The date @return {date} is converted from a string * * strturndate:function (str) {var re =/^ (\d{4}) \s (\d{1,2}) \s (\d{1,2}) $ /; var dt; if (Re.test (str)) {dt = new Date (regexp.$1,regexp.$2-1,regexp.$3); } return DT; },/* Method action: "Calculate days between 2 dates" * Incoming format: YYYY-MM-DD (2015-01-31) * Method of Use: Dateutil.dayminus (startdate,enddate); * @startDate {Date} start date * @endDate {date} end date * @return enddate-startdate difference in days */Dayminus:function (STARTD Ate, EndDate) {if (startdate instanceof date && endDate instanceof date) {var days = Math.floor (( Enddate-startdate)/(1000 * 60 * 60 * 24)); return days; }else{return "Param error,date type!"; }}};/*********************************************************************** * Load Tool class * * Note: Call mode, Loadutil. Method Name * * ********************/var loadutil = {/* * Method Description: "Dynamically load JS file css File" * Method of Use: Loadutil.loa Djscssfile ("Http://libs.baidu.com/jquery/1.9.1/jquery.js", "JS") * @param fileurl file path, * @param filetype file type, support incoming Type, JS, CSS */loadjscssfile:function (Fileurl,filetype) {if (filetype = = "js") {var fileref = Docume Nt.createelement (' script '); Fileref.setattribute ("type", "Text/javascript"); Fileref.setattribute ("src", fileurl); }else if (filetype = = "css") {var fileref = document.createelement (' link '); Fileref.setattribute ("rel", "stylesheet"); Fileref.setattribute ("type", "text/css"); Fileref.setattribute ("href", fileurl); } if (typeof fileref! = "undefined") {document.getElementsByTagName ("head") [0].appendchild (FILEREF); }else{alert ("Loadjscssfile method error!"); } }};/************************************************* String Manipulation Tool class * * Note: Call method, Strut Il. Method name * * ********************************************************************/var strutil = {/* * Determine if string is empty * @param str passed in String * @returns {} */Isempty:function (str) {if (str! = NULL && Str.length > 0) {return true; }else{return false; }},/* * Determine two strings of the same * @param str1 * @param str2 * @returns {Boolean} */Isequals:function (s TR1,STR2) {if (STR1==STR2) {return true; }else{return false; }},/* Ignore case to determine if string is the same * @param str1 * @param str2 * @returns {Boolean} */Isequalsignoreca Se:function (STR1,STR2) {if (str1.touppercase () = = Str2.touppercase ()) {return true; }else{return false; }},/** * Determines if it is a number * @param value * @returns {Boolean} */Isnum:function (value) {if (value! = null && value.length>0 && IsNaN (value) = = False) {return true; }else{return false; }},/** * Determines if it is Chinese * @param str * @returns {Boolean} */Ischine:function (str) {var reg =/ ^ ([u4e00-u9fa5]| [Ufe30-uffa0]) *$/; if (Reg.test (str)) {return false; } return true; }};
Copy Code
JS Common tool class.