const NUM_9 = 9;
// 生成32位id
function randomBqId() {
const len = 32;
const chars = 'abcdefhijkmnprstwxyz123456789';
const maxPos = chars.length;
let bqId = '';
for (let i = 0; i < len; i++) {
bqId += chars.charAt(Math.floor(Math.random() * maxPos));
}
return bqId;
}
// 获取当前时间(精确到分钟)
function getCurrentDate() {
const date = new Date();
let month = date.getMonth() + 1;
let strDate = date.getDate();
let strHours = date.getHours();
let strMinutes = date.getMinutes();
if (month >= 1 && month <= NUM_9) {
month = `0${month}`;
}
if (strDate >= 0 && strDate <= NUM_9) {
strDate = `0${strDate}`;
}
if (strHours >= 0 && strHours <= NUM_9) {
strHours = `${strHours}`;
}
if (strMinutes >= 0 && strMinutes <= NUM_9) {
strMinutes = `${strMinutes}`;
}
return `${date.getFullYear()}年${month}月${strDate}日${strHours}时${strMinutes}分`;
}
----------更改:
// 生成随机数
function S4() { // eslint-disable-line
// 0x 16禁止 0八进制 0b 2进制 b=binary
return (((1 + Math.random()) * 0x10000) | 0).toString(16) // eslint-disable-line
.substring(1); // eslint-disable-line
}
// 获取uuid 32位
getUUID() {
return S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4();
},
// 获取当前时间
getCurrentDateTimeStr(hasSecond) {
const _date = new Date();
// 获取日期
const _dateStr = _date.toLocaleDateString().split('/');
const _timeStr = _date.toTimeString().split(' ')[0].split(':');
// 组合时间日期
let _dateTimeStr = `${_dateStr[0]}年${this.transferTime(_dateStr[1])}月${this.transferTime(_dateStr[2])}日${_timeStr[0]}时${_timeStr[1]}分`; // eslint-disable-line
// 如果需要秒
if (hasSecond) {
_dateTimeStr = _dateTimeStr + `${_timeStr[2]}秒`;// eslint-disable-line
}
return _dateTimeStr;
},
// 获取当前系统时间
getTime() {
const myDate = new Date();
// 获取完整的年份(4位,1970-????)
const myYear = myDate.getFullYear();
// 获取当前月份(0-11,0代表1月)
const myMonth = myDate.getMonth() + 1;
// 获取当前日(1-31)
const myToday = myDate.getDate();
// 获取当前星期X(0-6,0代表星期天)
// const myDay = myDate.getDay();
// 获取当前小时数(0-23)
const myHour = myDate.getHours();
// 获取当前分钟数(0-59)
const myMinute = myDate.getMinutes();
// const mySecond = myDate.getSeconds(); // 获取当前秒数(0-59)
// const week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
return `${String(`${myYear}年${this.transferTime(myMonth)}月${this.transferTime(myToday)}日`) + this.transferTime(myHour)}时${this.transferTime(myMinute)}分`;
},
// 转换时间
transferTime(str) {
let realNum;
if (str < 10) { // eslint-disable-line
realNum = `0${str}`;
} else {
realNum = str;
}
return realNum;
}