// 随机生成带有时间得32位数字流水号
export function getMathNum() {
const myDate = new Date() // 当前中国标准时间
const year = myDate.getFullYear() // 获取当前年份 支持IE和火狐浏览器.
const month = myDate.getMonth() + 1 // 获取中国区月份
const day = myDate.getDate() // 获取几号
let milliseconds = myDate.getMilliseconds(); //获取当前毫秒数(0-999)
let seconds = myDate.getSeconds();
var currentDate = year;
currentDate += prefixZero(month,2);
currentDate += prefixZero(day,2);
currentDate += prefixZero(seconds,2);
currentDate += prefixZero(milliseconds,3);
var timestamp = (new Date().getTime()) + ''; //当前时间戳
// 生成随机6位数,补齐32位
var mm = Math.random();
var mathNum = "";
if (mm > 0.1) {
mathNum = Math.round(mm * 1000000);
} else {
mm += 0.1;
mathNum = Math.round(mm * 1000000);
}
return `${currentDate}${timestamp}${mathNum}`;
}
console.log(getMathNum(),getMathNum().length)
/*
*给数字前面补0
*num 数字
*length 数字需要的长度
*/
export function prefixZero(num,length){
return (Array(length).join(0) + num).slice(-length);
}