js------生成32位id;获取当前时间

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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值