/**
* [获取url参数https://a.com?a=1&b=2获取a,b的值]
* @param {[type]} name [参数名(a,b)]
* @return {[type]} [得到的值(1,2)]
*/
function getQueryString(name){
// 构造一个含有目标参数的正则表达式对象
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
// 匹配目标参数
// let urls=window.location.search.substr(1);
let urls = window.location.href.split('?')[1];
if (urls) {
let r = urls.match(reg);
if (r != null) {
return decodeURI(r[2]);
}
}
return null;
}
/**
* [description]
* @param {[type]} times 时间戳,1535701322000这种格式
* @param {[type]} format 时间格式,默认为[yyyy-MM-dd hh:mm:ss],注意大小写
* @return {[type]} date [description]
*/
function FormatDate(times, format = 'yyyy-MM-dd hh:mm:ss') {
times = new Date(times)
let date = {
'M+': times.getMonth() + 1,
'd+': times.getDate(),
'h+': times.getHours(),
'm+': times.getMinutes(),
's+': times.getSeconds(),
'q+': Math.floor((times.getMonth() + 3) / 3),
'S+': times.getMilliseconds(),
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (times.getFullYear() + '')
.substr(4 - RegExp.$1.length));
}
for (let k in date) {
if (new RegExp('(' + k + ')').test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length === 1
? date[k] : ('00' + date[k]).substr(('' + date[k]).length));
}
}
return format;
}
/**
* [金额格式化]
* @param {[type]} number [要格式化的数分]
* @param {[type]} isMoney [传进来的金额单位为分,默认为true]
* @param {[type]} thousands_sep [千分位符号,默认为,]
* @param {[type]} decimals [保留几位小数,默认为2]
* @param {[type]} dec_point [小数点符号,默认为.]
* @return {[type]} [格式化之后的金额元]
*/
function FormatPrice(number, isMoney = true, decimals = 2, thousands_sep = ',',
dec_point = '.'){
// 先转为元
if (isMoney) number = (parseInt(number, 10) / 100).toFixed(decimals);
else {
number = parseInt(number, 10);
}
number = (number + '').replace(/[^0-9+-Ee.]/g, '');
let n = !isFinite(+number) ? 0 : +number;
let prec = !isFinite(+decimals) ? 0 : Math.abs(decimals);
let sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
let dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
let s = '';
let toFixedFix = function (n, prec) {
let k = Math.pow(10, prec);
// 防止精度缺失,要加0.5
return '' + Math.floor(n * k + 0.5) / k;
};
s = (prec ? toFixedFix(n, prec) : '' + Math.floor(n)).split('.');
let re = /(-?\d+)(\d{3})/;
while (re.test(s[0])) {
s[0] = s[0].replace(re, "$1" + sep + "$2");
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
return s.join(dec);
}
/**
* [获取时间区间]
* @param {[type]} type [获取时间区间格式,today今日,week本周,month本月,year今年]
* @return {[type]} [description]
*/
function getTimeDistance(type){
const now = new Date();
const oneDay = 1000 * 60 * 60 * 24;
const fixedZero = (val) => {
return val * 1 < 10 ? `0${val}` : val;
}
if (type === 'today') {
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
return [moment(now), moment(now.getTime() + (oneDay - 1000))];
}
if (type === 'week') {
let day = now.getDay();
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
if (day === 0) {
day = 6;
} else {
day -= 1;
}
const beginTime = now.getTime() - day * oneDay;
return [moment(beginTime), moment(beginTime + (7 * oneDay - 1000))];
}
if (type === 'month') {
const year = now.getFullYear();
const month = now.getMonth();
const nextDate = moment(now).add(1, 'months');
const nextYear = nextDate.year();
const nextMonth = nextDate.month();
return [
moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`),
moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000),
];
}
if (type === 'year') {
const year = now.getFullYear();
return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
}
}
/**
*
* @param {[type]} ms 毫秒值
* @returns 天/时/分/秒
*/
function formatDuring(ms){
const days = parseInt(ms / (1000 * 60 * 60 * 24));
const hours = parseInt((ms % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = parseInt((ms % (1000 * 60 * 60)) / (1000 * 60));
const seconds = (ms % (1000 * 60)) / 1000;
if (days > 0) {
return `${days}天${hours}小时${minutes}分${seconds}秒`
} else if (hours > 0) {
return `${hours}小时${minutes}分${seconds}秒`
} else if (minutes > 0) {
return `${minutes}分${seconds}秒`
} else {
return `${seconds}秒`
}
}
/**
*
* @param {*} targetTime 目标时间戳
* @param {*} nowTime 指定时间/当前时间
* @returns
*/
function FormatCountdown(targetTime, nowTime = (new Date).getTime()){
var stime = targetTime;
var etime = nowTime;
var usedTime = stime - etime; //两个时间戳相差的毫秒数
if (usedTime > 0) {
var days = Math.floor(usedTime / (24 * 3600 * 1000));
//计算出小时数
var leave1 = usedTime % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
var hours = Math.floor(leave1 / (3600 * 1000)) < 10 ? '0' + Math.floor(leave1 / (3600 * 1000)) : Math.floor(leave1 / (3600 * 1000));
//计算相差分钟数
var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
var minutes = Math.floor(leave2 / (60 * 1000)) < 10 ? '0' + Math.floor(leave2 / (60 * 1000)) : Math.floor(leave2 / (60 * 1000));
//计算剩余秒数
var leave3 = leave2 % (60 * 1000);
var seconds = Math.floor(leave3 / 1000) < 10 ? '0' + Math.floor(leave3 / 1000) : Math.floor(leave3 / 1000);
if (days > 0) {
var time = days + "天" + hours + ":" + minutes + ":" + seconds;
return time;
} else {
var time = hours + ":" + minutes + ":" + seconds;
return time;
}
} else {
return '';
}
}
/**
* 递归查找树状结构中的制定属性值
* @param {*} list 要查找的树状结构
* @param {*} id 要查找的树状结构的依据
* @param {*} val 需要的结果val
* @returns
*/
function renderSelectChildName(list, id, val) {
let value = null;
for (let index = 0; index < list.length; index += 1) {
if (list[index].id == id) {
value = list[index][val];
break;
}
if (list[index].children instanceof Array && list[index].children.length > 0) {
const text = this.renderSelectChildName(list[index].children, id, val);
if (text) {
return text;
}
}
}
return value;
}
/**
* 比较两个时分数据的大小 HH:mm
* @param {*} t1 t2 要比较的数据
* @returns boolean
*/
function CompareDate(t1, t2){
var date = new Date();
var a = t1.split(":");
var b = t2.split(":");
return date.setHours(a[0], a[1]) < date.setHours(b[0], b[1]);
}
}
/**
* 字符串填充函数
* @param {string} value 目标字符串
* @param {array} position 需要填充的位置
* @param {string} padstr 填充字符串
* @return {string} 返回目标字符串
*/
function padStr(value, position, padstr, inputElement){
position.forEach((item, index) => {
if (value.length > item + index) {
value = value.substring(0, item + index) + padstr + value.substring(item + index)
}
})
value = value.trim();
// 解决安卓部分浏览器插入空格后光标错位问题
requestAnimationFrame(() => {
inputElement.setSelectionRange(value.length, value.length);
})
return value;
}
/**
* 验证的正则表达式
*/
// 手机号
mobile: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
//车牌号
carNo:/([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})/,
//座位数
seatCount: /^([4-9]{1}|[1-9][0-9]{1}|100)$/ ,
//身份证
IDnumber: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
// 不能输入汉字
passwordM: /^[^\u4e00-\u9fa5]{0,}$/
超好用的前端数据处理方法【1】
最新推荐文章于 2024-07-21 22:01:34 发布