千位分割函数(,隔开):
private a (value:any) {
let number = null;
let decimalPart = '';
value = value + '';
if (value.indexOf('.') !== -1) {
decimalPart = '.' + value.split('.')[1];
value = parseInt(value.split('.')[0]);
}
const array = value.toString().split('');
let index = -3;
while (array.length + index > 0) {
array.splice(index, 0, ',');
index -= 4;
}
number = array.join('') + decimalPart;
return number;
}
删除功能:
// arr: 数组名 ;index: 删除第几项(下标)
arr.splice(index, 1);
常用正则表达式:
// /^[\u4E00-\u9FA5]+$/ 纯中文
const reg = /^[\u4E00-\u9FA5]+$/;
// 测试 text 是不是纯中文,返回布尔值
const regres = reg.test(text);
// /^\d{1,}$/ 纯数字 /^[a-zA-Z]+$/ 纯英文
const regnum = /^\d{1,}$/;
const regyw = /^[a-zA-Z]+$/;
const regresnum = regnum.test(text);
const regresyw = regyw.test(text);
日期推算:
// 日期转换函数
private formatDate (date: any) {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return (
year +
'-' +
(String(month).length > 1 ? month : '0' + month) +
'-' +
(String(day).length > 1 ? day : '0' + day)
);
}
// 今天
private date: string = new Date().toISOString().substr(0, 10);
// 距离今天的第前7天
private sevdate: string = new Date((new Date().getTime() - (6 * 86400000))).toISOString().substr(0, 10);
private startTime: string = this.sevdate;
private endTime: string = this.date;
// 昨天
private yesterday: string = new Date((new Date().getTime() - 86400000)).toISOString().substr(0, 10);
// 上周开始
// 今天是周日
private lastweekstart: string = new Date((new Date().getTime() - ((7 + 6) * 86400000))).toISOString().substr(0, 10);
// 今天是周一~周六
private lastweekstarts: string = new Date((new Date().getTime() - ((new Date().getDay() + 6) * 86400000))).toISOString().substr(0, 10);
// 上周结束
private lastweekend: string = new Date((new Date().getTime() - (new Date().getDay() * 86400000))).toISOString().substr(0, 10);
// 上月开始
private lastmonthstart: string = new Date(new Date().getTime() - (((new Date(new Date((new Date().getTime() - (new Date().getDate() * 86400000)))).getDate()) - 1 + new Date().getDate()) * 86400000)).toISOString().substr(0, 10);
// 上月结束
private lastmonthend: string = new Date((new Date().getTime() - (new Date().getDate() * 86400000))).toISOString().substr(0, 10);
检查value 是否是 null 或者undefined:
isNil(value)
循环按钮组:
// 结构:vuetify
<v-btn
v-for="(item,index) in cyclelist"
:key="index"
small
:class="cyclenumber === index?'btnsel':'btnnot'"
@click="btntar(index)"
>{{item}}</v-btn>
// 样式:scss
.btnsel {
color: white;
width: 105px;
font-size: 14px;
background-color: rgba(51, 102, 153, 0.5) !important;
}
.btnnot {
color: #000;
width: 105px;
font-size: 14px;
}
// 行为:ts
private cyclelist: string[] = ['实时', '当日', '自定义'];
private cyclenumber: number = 0;
private btntar (info:number) {
this.cyclenumber = info;
};