一个随时更新的js库
1.src同级建commFunction=》timer.js
2.main.js引入
import time from '../commonFunction/time'
Vue.prototype.time=time
=======================================
情况一1.时间戳
// 时间戳转时间 2020-4-24 00:30:00
timeStamp(val) { val = val.toString() if(val.length == 10) { let time = val * 1000; let timestamp4 = new Date(time); return (timestamp4.toLocaleDateString().replace(/\//g, "-") +" " + timestamp4.toTimeString().substr(0, 8)); } else { let timestamp4 = new Date(val); return (timestamp4.toLocaleDateString().replace(/\//g, "-") +" " +timestamp4.toTimeString().substr(0, 8)); } }, // 2017-4-8 14:11:33转2017-04-08 14:11:33 handeldate(m){ let o = m[0]+m[1]+m[2]+m[3] if(m.length==18){ if(m[6]=='-'){ return o+m[4]+'0'+m[5]+m[6]+m[7]+m[8]+m.slice(9) } if(m[7]=='-'){ return o+m[4]+m[5]+m[6]+m[7]+'0'+m[8]+m.slice(9) } }else if(m.length==17){ return o+m[4]+'0'+m[5]+m[6]+'0'+m[7]+m.slice(8) }else{ return m } }
页面使用:
appList.forEach(element => { element.create_time = this.time.handeldate(this.time.timeStamp(element.create_time)) });
=======================================
二,生成订单号HWsdAjkFlvh...152652366(后边是时间戳,头部可自定义)
createOrder(){ //当前时间戳 let stamp = Date.parse(new Date()).toString() let orderEnd = '' let letter = 'A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m' let letterStr = letter.split(',') let letLength = letterStr.length //设置订单号后半截随机的长度 for(let i=0;i<10;i++){ orderEnd+=letterStr[this.randomNum(0,letLength-1)] } return 'HW'+orderEnd+stamp.substr(0,10) },
//随机数0-n
randomNum(minNum,maxNum){
switch(arguments.length){
case 1:
return parseInt(Math.random()*minNum+1,10);
break;
case 2:
return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
break;
default:
return 0;
break;
}
},
=======================================
三,生成6位随机码8b1BQl(需要上边的随机数方法)
createShortLink(){ let shortLink = '' let letter = 'A,B,C,E,F,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0,q,a,e,r,t,y,u,o,p,g,h,j,k,l,x,b,n,m,1,2,3,4,5,6,7,8,9,0' let letterStr = letter.split(',') let letLength = letterStr.length for(let i=0;i<6;i++){ shortLink+=letterStr[this.randomNum(0,letLength-1)] } return shortLink }
=======================================
四,做echart图表的几个跟日期有关的方法
//输出相同元素的个数
export function unique (arr) {
var resultObj = {};
var result = [];
var result2 = [];
for(var i=0; i<arr.length; i++){
if(result.indexOf(arr[i]) == -1){
result.push(arr[i]);
}else{
if(result2.indexOf(arr[i]) == -1){
result2.push(arr[i]);
}
}
}
var obj = {}
arr.forEach((v,k)=>{
if(obj[v]){
obj[v]++;
}else{
obj[v] = 1;
}
});
resultObj.result = result;
resultObj.result2 = result2;
resultObj.obj = obj;
return {resultObj}
}
//获取最近day天日期数组,含今天(x横坐标)
export function getBeforeDateLineX(day){
var ot = new Date().getTime(),
da = [],
nd,
month,
day;
for(var i=day-1;i>-1;i--){
nd = ot - i*24*60*60*1000;
month = new Date(nd).getMonth()+1,
day = new Date(nd).getDate();
da.push(month+'.'+day);
}
return da;
}
//获取最近n天的两个端点时间戳,当前时刻至n天前的当前时刻(含今天,即时)
export function getTimePoint(n){
let value1 = []
let dateToDay = parseInt(new Date().getTime()/1000)
value1[0] = dateToDay - n*24*60*60
value1[1] = dateToDay
return value1
}
//获取最近n天的两个端点时间戳,当前天至n天前(不含今天,整点)
export function getTimePointZero(n){
let today = new Date();
let dateToDay = parseInt(new Date(today.toDateString()).getTime()/1000);
let value1 = []
value1[0] = dateToDay - n*24*60*60
value1[1] = dateToDay
return value1
}
//获取前第n天的初末时刻
export function beforeYesterdayZero(n){
let today = new Date();
let dateToDay = parseInt(new Date(today.toDateString()).getTime()/1000);
let value1 = []
value1[0] = dateToDay - n*24*60*60
value1[1] = dateToDay - (n-1)*24*60*60
return value1
}
//获取最近日期数组,可不含今天(x横坐标)参数是某两天00:00的10位时间戳
export function getGroupDateLineX(tZero1,tZero2){
let a = tZero1
let arrStamps = [tZero1]
for(let i=0;i<100;i++){
if(a<tZero2){
a+=24*60*60
arrStamps.push(a)
}
}
return arrStamps
}
//输出数组相同元素个数
export function getCount(arr,rank,ranktype){
var obj = {}, k, arr1 = [];
for (var i = 0, len = arr.length; i < len; i++) {
k = arr[i];
if (obj[k])
obj[k]++;
else
obj[k] = 1;
}
//保存结果{el-'元素',count-出现次数}
for (var o in obj) {
arr1.push({el: o, count: obj[o]});
}
//排序(降序)
arr1.sort(function (n1, n2) {
return n2.count - n1.count
});
//如果ranktype为1,则为升序,反转数组
if(ranktype===1){
arr1=arr1.reverse();
}
var rank1 = rank || arr1.length;
return arr1.slice(0,rank1);
}