一些js方法

生成uuid

exportfunction guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); }function UUID (){ var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; var uuid = s.join(""); return uuid; }

隐藏手机号
filterTel(tel) { if (tel && tel.length == 11) { let phone = tel; let myphone = phone.substr(3, 4); phone = phone.replace(myphone, "****"); return phone; } return tel; }

身份证隐藏年月日
function formatidcard(idcard) { // 15位身份证举例:130503 670401 001的含义; 13为河北,05为邢台,03为桥西区,出 生日期为1967年4月1日,顺序号为001。 if(idcard.length == 15){ return idcard.replace(/(\d{6})\d{6}(\d{3})/, "$1******$2"); } else { return idcard.replace(/(\d{5})\d{6}(\d{6})/, "$1******$2"); } }

判断是否是微信
function isWeixin() { var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i)=="micromessenger") { return true; } else { return false; } }

移动端浏览器内核判断
function browserVersions(){ var u = navigator.userAgent, app = navigator.appVersion; return {//移动终端浏览器版本信息 trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端 ios: !!u.match(/\(i[^; ]+; ( U; )? CPU.+Mac OS X/), //ios终端 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器 iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部 }; }

一些日期相关的方法
// 获取年加减 export function nowDate(e) { let d = new Date(); let m = d.getMonth() + 1; let date = d.getDate(); m = m > 9 ? m : '0' + m; date = date > 9 ? date : '0' + date; if (e) { return (d.getFullYear() + e) + '/' + m + '/' + date; } else { return d.getFullYear() + '/' + m + '/' + date; } } // 日期格式化 export function parseTime(time, cFormat) { 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 (typeof time == 'number') { if(('' + time).length === 10){ time = parseInt(time) * 1000 } } else if (time.indexOf('-') != -1) { time = time.replace(/\-/g, '/'); } 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 = https://www.it610.com/article/formatObj[key] if (key ==='a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1] if (result.length > 0 && value < 10) { value = 'https://www.it610.com/article/0' + value } return value || 0 }) return time_str }// 获取本周第i天的日期 export function getWeek(i,formate='-') { var now = new Date(); var firstDay = new Date(now - (now.getDay() - 1) * 86400000); firstDay.setDate(firstDay.getDate() + i); let mon = Number(firstDay.getMonth()) + 1; return firstDay.getFullYear() + formate + mon + formate + firstDay.getDate(); }// 获取当前月的第一天和最后一天 export function getMonthDate(num=0){ let date = new Date(); let currentMon = date.getMonth(); date.setMonth(currentMon+num,1); let startDate = parseTime(date,'{y}-{m}-{d}'); date.setMonth(currentMon+1+num,0); let endDate = parseTime(date,'{y}-{m}-{d}'); return { startDate, endDate }; }// 加减天 export function addDay(nowDate,num=0){ let now = nowDate.getTime(); now += num*24*3600*1000; let date = parseTime(new Date(now),'{y}-{m}-{d}'); return date; }

    推荐阅读