获取今天日期
const getToday = () => {
let date = new Date()
let year = date.getFullYear()
let month = date.getMonth() + 1
month = month < 10 ? `0${month}` : month
let day = date.getDate()
day = day < 10 ? `0${day}` : day
let dates = `${year}-${month}-${day}`
return dates
}
获取今天的前n天日期
const getBeforeDate = (n) => {
var date = new Date();
var year, month, day;
date.setDate(date.getDate() - n);
year = date.getFullYear();
month = date.getMonth() + 1;
day = date.getDate();
return year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day);
}
千位逗号分割
/**
* @param {*} num 数字
* @param {*} fixed 保留几位小数
*/
const splitThousand = (num, fixed) => {
if (num) {
if (typeof num !== "number") {
num = parseFloat(num)
}
var reg = /\B(?=(\d{3})+$)/g
num = num.toString().split(".")
fixed = fixed == undefined ? 2 : fixed
num[0] = num[0].replace(reg, ",")
num[1] = num[1] ? num[1].substr(0, fixed) : "00000000000000000".substr(0, fixed)
if (num[1].length === 1) {
num[1] = `${num[1]}0`
}
return fixed ? num.join(".") : num[0]
}
}
节流,防止多次点击
const throttle = (fn, wait = 1500) => {
// 上一次执行 fn 的时间
let previous = 0
// 将 throttle 处理结果当作函数返回
return function(...args) {
// 获取当前时间,转换成时间戳,单位毫秒
let now = +new Date()
// 将当前时间和上一次执行函数的时间进行对比
// 大于等待时间就把 previous 设置为当前时间并执行函数 fn
if (now - previous > wait) {
previous = now
fn.apply(this, args)
}
}
}
防抖
export function debounce(func, wait = 1000) {
let timeout;
return function () {
let context = this;
let args = arguments;
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(context, args)
}, wait);
}
}
金钱正则
const isMoney = (val) => {
const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
if (reg.test(val)) {
return true
} else {
return false
}
}
判断数据类型
const nonEmpty = (tgt) => {
if (DataType(tgt, 'array')) {
return JSON.stringify(tgt) !== '[]'
} else if (DataType(tgt, 'object')) {
return JSON.stringify(tgt) !== '{}'
}
}
给一串数字添加‘*’,比如身份证或银行卡隐藏某些文字
/**
* @param {*} str 字符串
* @param {*} frontLen 前面保留位数
* @param {*} endLen 后面保留位数
*/
const plusXing = (str, frontLen = 10, endLen = 4) => {
var len = str.length-frontLen-endLen;
var xing = '';
for (var i=0;i<len;i++) {
xing+='*';
}
return str.substr(0,frontLen)+xing+str.substr(str.length-endLen);
}
判断一个值是否为空
/**
* 如果是undefined,null,'',NaN,false,0,[],{},空白字符串,都返回true,否则返回false
* @param {*} v 被判断的值
*/
const isEmpty = (v) => {
switch (typeof v) {
case 'undefined':
return true;
case 'string':
if (v.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
break;
case 'boolean':
if (!v) return true;
break;
case 'number':
// eslint-disable-next-line no-restricted-globals
if (0 === v || isNaN(v)) return true;
break;
case 'object':
if (null === v || v.length === 0) return true;
for (var i in v) {
return false;
}
return true;
}
return false;
}
localStorage 存贮
/**
* @param { String } key 属性
* @param { string } value 值
*/
export const localStorageSet = (key, value) => {
if (typeof (value) === 'object') {
value = JSON.stringify(value)
};
localStorage.setItem(key, value)
};
localStorage 获取
/**
* @param {String} key 属性
*/
export const localStorageGet = (key) => {
return localStorage.getItem(key)
};