【前端实用工具】一些实用函数记录
-
- 1.返回一个对象里面包含后一个数组比第一个数组增加、减少的数据
- 2.计算天数差
- 3.获取当月收尾日期
- 4.获取当前日期前n天
- 5.时间选择范围
- 6.RGB颜色值转换成十六进制颜色码
- 7.检查密码强度,判断是否弱密码
不定时更新,有的在以前的帖子不做搬运。
如有更好的写法请联系我!!!
在utils.js文件里声明然后在需要的地方引用即可
1.返回一个对象里面包含后一个数组比第一个数组增加、减少的数据
export function compare(beforeArr, afterArr) {
let resObj = {
add: [],
del: []
},
cenObj = {};
for (let i = 0; i < beforeArr.length; i++) {
cenObj[beforeArr[i]] = beforeArr[i];
}
for (let j = 0; j < afterArr.length; j++) {
if (!cenObj[afterArr[j]]) {
resObj.add.push(afterArr[j]);
} else {
delete cenObj[afterArr[j]]
}
}
for (k in cenObj) {
resObj.del.push(k);
}
return resObj;
}
2.计算天数差
export function DateDiff(sDate1, sDate2) {
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
aDate = sDate2.split("-")
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)
return iDays
}
3.获取当月收尾日期
export function getFullDate(day) {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var sDate = new Date(year, month, 1)
var eDate = new Date(year, month + 1, 0)
return [sDate, eDate]
}
4.获取当前日期前n天
export function getNDate(day) {
var date = new Date();
var base = new Date(date).getTime();
var oneDay = 24 * 3600 * 1000;
var date = [];
var data = [Math.random() * 300];
var time = new Date(base);
date.push([time.getFullYear(), time.getMonth() + 1, time.getDate()].join('-'));
for (var i = 1; i < day; i++) {
var now = new Date(base -= oneDay);
date.push([now.getFullYear(), (now.getMonth() + 1) >= 10 ? (now.getMonth() + 1) : '0' + (now.getMonth() + 1), now.getDate() >= 10 ? now.getDate() : '0' + now.getDate()].join('-'));
data.push(Math.round((Math.random() - 0.5) * 20 + data[i - 1]));
}
var newdate = date.reverse();
return newdate
}
5.时间选择范围
export function getBeforeDate(strDate, n) {
var datt = strDate.split('-');
var newDate = new Date(datt[0], datt[1] - 1, datt[2]);
var befminuts = newDate.getTime() + 1000 * 60 * 60 * 24 * parseInt(n);
var beforeDat = new Date;
beforeDat.setTime(befminuts);
var befMonth = beforeDat.getMonth() + 1;
var mon = befMonth >= 10 ? befMonth : '0' + befMonth;
var befDate = beforeDat.getDate();
var da = befDate >= 10 ? befDate : '0' + befDate;
var newDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
return newDate;
}
6.RGB颜色值转换成十六进制颜色码
export function getColor(hexColor, alpha = .5) {
var hex = hexColor.replace('#', '');
var r = parseInt(hex.substring(0, 2), 16);
var g = parseInt(hex.substring(2, 4), 16);
var b = parseInt(hex.substring(4, 6), 16);
var rgba = `rgba(${r}, ${g}, ${b}, ${alpha})`
return rgba;
}
7.检查密码强度,判断是否弱密码
export function checkPwdStrong(pwd) {
let score = 0
if ((!pwd || pwd.length < 6) || (!/^(?=.*[0-9].*)(?=.*[a-z].*)(?=.*[A-Z]).{6,20}$/.test(pwd))) {
return score
}
score++
if (/[_]/.test(pwd) || /^[A-Z]+$/.test(pwd)) {
score++
}
return score
}
<ul class="pwd-strong">
<li :class="{ weak: pwdLevel === 0 }">弱</li>
<li :class="{ better: pwdLevel === 1 }">中</li>
<li :class="{ strong: pwdLevel === 2 }">强</li>
</ul>
computed: {
pwdLevel() {
return checkPwdStrong(this.user.newPassword);
},
},