js基本函数
原创
©著作权归作者所有:来自51CTO博客作者IT枫斗者的原创作品,请联系作者获取转载授权,否则将追究法律责任
js基本函数
不定参数求和
SUM: function (...arg) {
let len = arg.length;
if (len > 0){
var sum = 0;
for(let i=0;i<len;i++){
sum += arg[i]
}
return sum
}
return ""
},
不定参数求平均值
AVG: function (...arg) {
let len = arg.length;
if (len > 0){
var sum = 0;
for(let i=0;i<len;i++){
sum += arg[i]
}
return sum/len
}
return ""
},
不定参数求最大值
MAX :function (...arg){
let len = arg.length;
if (len > 0){
var max = "Math.max(";
for(let i=0;i<len;i++){
max += arg[i] + ","
}
return eval(max.substr(0,max.length-1) +")")
}
return "";
},
不定参数求最小值
MIN: function (...arg){
let len = arg.length;
if (len > 0){
var min = "Math.min(";
for(let i=0;i<len;i++){
min += arg[i] + ","
}
return eval(min.substr(0,min.length-1) +")")
}
return "";
},
四舍五入精确到固定位小数
ROUND: function (...arg){
let len = arg.length;
if (len == 1){
return Math.round(arg[0] + 'e' + 2) / Math.pow(10, 2);
} else if (len == 2){
return Math.round(arg[0] + 'e' + arg[1]) / Math.pow(10, arg[1]);
} else {
return ""
}
},
获取日期类型参数年份 2006-10-01 12:00:05
YEAR: function (...arg){
let len = arg.length;
if (len == 1){
return new Date(arg[0].replace(/-/g, '/')).getFullYear();
}
return ""
},
获取日期类型参数月份 2006-10-01 12:00:05
MONTH: function (...arg){
let len = arg.length;
if (len == 1){
return new Date(arg[0].replace(/-/g, '/'))..getMonth();
}
return ""
},
获取日期类型参数日期 2006-10-01 12:00:05
DAY: function (...arg){
let len = arg.length;
if (len == 1){
return new Date(arg[0].replace(/-/g, '/'))..getDate();
}
return ""
},
获取日期类型参数小时 2006-10-01 12:00:05
HOUR: function (...arg){
let len = arg.length;
if (len == 1){
return new Date(arg[0].replace(/-/g, '/'))..getHours();
}
return ""
},
获取日期类型参数分钟 2006-10-01 12:00:05
MINUTE: function (...arg){
let len = arg.length;
if (len == 1){
return new Date(arg[0].replace(/-/g, '/'))..getMinutes();
}
return ""
},
获取日期类型参数秒数 2006-10-01 12:00:05
SECOND: function (...arg){
let len = arg.length;
if (len == 1){
return new Date(arg[0].replace(/-/g, '/'))..getSeconds();
}
return ""
},
返回当前的日期 2006-10-01
CURDATE: function (...arg){
var date = new Date()
var month = date.getMonth() + 1
var strDate = date.getDate();
if(date.getMonth() + 1 < 10){
month = "0" + (date.getMonth() + 1)
}
if(date.getDate() < 10){
strDate = "0" + date.getDate()
}
return date.getFullYear() + '-' + month + '-' + strDate
},
返回当前日期时间字符串 2006-10-01 12:00:05
NOW: function (...arg){
var date = new Date();
var month = date.getMonth() + 1;
var strDate = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
if(date.getMonth() + 1 < 10){
month = "0" + (date.getMonth() + 1)
}
if(date.getDate() < 10){
strDate = "0" + date.getDate()
}
if(date.getHours() < 10){
hour = "0" + date.getHours()
}
if(date.getMinutes() < 10){
minute = "0" + date.getMinutes()
}
if(date.getSeconds() < 10){
second = "0" + date.getSeconds()
}
return date.getFullYear() + '-' + month + '-' + strDate+ ' ' + hour + ':' + minute + ':' + second
},
将两个字符串连接为一个字符串
CONCAT: function (...arg){
let len = arg.length;
if (len == 2){
return arg[0].concat(arg[1]);
}
return ""
},
返回文本字符串中从指定位置开始的特定数目的字符
SUBSTR: function (...arg){
if (arg.length == 3){
return arg[0].substr(arg[1], arg[2]);
} else if (arg.length == 2){
return arg[0].substr(arg[1], arg[0].toString().length);
}
return "";
},
字符串的长度
LEN: function (...arg){
if (arg.length == 1){
return arg[0].length;
}
return "";
},
字符串转大写
UPPER: function (...arg){
if (arg.length == 1){
return arg[0].toUpperCase();
}
return "";
},
字符串转小写
LOWER: function (...arg){
if (arg.length == 1){
return arg[0].toLowerCase();
}
return "";
},
替换字符串
REPLACE: function (...arg){
if (arg.length == 3){
return eval("\""+arg[0] + "\"" + ".replace(" + "/" +arg[1] +"/g"
+", "+ "\""+ arg[2] + "\"" + ")");
}
return "";
},
按字符截取字段为数组,并返回数组前n个元素
SPLIT: function (...arg){
if (arg.length == 3){
return arg[0].split(arg[1], arg[2]);
} else if (arg.length == 2){
return arg[0].split(arg[1], arg[0].length);
}
return "";
},