汇总一下做项目常用到的方法:
获得地址栏中的参数
function getUrlParam (id, url) {
var reg = "/(\\?|\\&|\\&&)" + id + "=([^\\&]+)/im";
reg = eval(reg);
var result = url.match(reg);
if (result && result[2]) {
return result[2];
} else {
return null;
}
}
去除字符串空格
function clearSpace (str){
var result;
result = str.replace(/(^\s+)|(\s+$)/g,"");
result = result.replace(/\s/g,"");
return result;
};
日期比较
function compareDate(start, end) {
console.info(start+" "+end);
start = new Date(start);
end = new Date(end);
if (end - start < 0) {
alert("结束日期要大于开始日期");
return false;
}
return true;
}
系统时间转换成‘yyyy-mm-dd’格式
function formatter(date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
};
系统时间转换成‘yyyy-mm-dd’格式转换成‘yyyy-mm-dd’格式
function ChangeDateFormat(val) {
if (val != null) {
var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
//月份为0-11,所以+1,月份小于10时补个0
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return date.getFullYear() + "-" + month + "-" + currentDate;
}
return "";
}
将金额格式化
function dealNumber(money) {
if (money && money != null) {
money = String(money);
var left = money.split(".")[0],
right = money.split(".")[1];
right = right
? right.length >= 2 ? "." + right.substr(0, 2) : "." + right + "0"
: ".00";
var temp = left
.split("")
.reverse()
.join("")
.match(/(\d{1,3})/g);
return (
(Number(money) < 0 ? "-" : "") + temp.join(",").split("").reverse().join("") + right);
} else if (money === 0) {
//注意===在这里的使用,如果传入的money为0,if中会将其判定为boolean类型,故而要另外做===判断
return "0.00";
} else {
return "";
}
};
时间戳转换成指定的时间格式
function parseTime(time, cFormat) {
if (time < 0) {
// t< 0 就返回当前时间
const t = new Date()
time = t.getTime()
}
if (arguments.length === 0) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if (('' + time).length === 10) time = parseInt(time) * 1000
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
if (key === 'a') { return ['一', '二', '三', '四', '五', '六', '日'][value - 1] }
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return time_str
}
根据时间戳显示距离当前时间多久
function formatTime(time, option) {
time = +time * 1000
const d = new Date(time)
const now = Date.now()
const diff = (now - d) / 1000
if (diff < 30) {
return '刚刚'
} else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + '分钟前'
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小时前'
} else if (diff < 3600 * 24 * 2) {
return '1天前'
}
if (option) {
return parseTime(time, option)
} else {
return (
d.getMonth() +
1 +
'月' +
d.getDate() +
'日' +
d.getHours() +
'时' +
d.getMinutes() +
'分'
)
}
}
获取文件名,不带后缀
function sliceFileName(file_path) {
return file_path.substring(0,file_path.lastIndexOf("."))
}
不允许存在添加重复的元素
function isAlreadyHasOne(jsonArray = [], item = {}, key) {
return jsonArray.find(i => i[key] === item[key])
}
数组根据某个元素进行升序或者
function sortJSONArrayByKey(jsonArray = [], key) {
return jsonArray.sort((i, j) => j[key] - i[key])或
return jsonArray.sort((i, j) => i[key] - j[key])
}