码迷,mamicode.com
首页 > Web开发 > 详细

js 时间戳与日期之间的转换 随机字符串

时间:2021-01-11 11:17:08      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:tno   lan   字符串   时间   logs   target   log   targe   字符   

 本文出自:https://www.cnblogs.com/2186009311CFF/p/14253131.html

//timestamp时间戳
const getTimestamp = () => {
    var timestamp = new Date().getTime(); //精确到毫秒
    return timestamp
}

//时间戳转化成时间
const getTimesByTamp = (timestamp) => {
    var ttamp = timestamp;
    if (ttamp.length == 10) {
        ttamp = ttamp * 1000;
    }
    var date = new Date(ttamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + ‘-‘;
    var M = (date.getMonth() + 1 < 10 ? ‘0‘ + (date.getMonth() + 1) : date.getMonth() + 1) + ‘-‘;
    var D = date.getDate() ;
    var h = date.getHours() ;
    var m = date.getMinutes();
    var s = date.getSeconds();
    if (D<10) {
        D=‘0‘+D;
    }
    if (h<10) {
        h=‘0‘+h;
    }
    if (m<10) {
        m=‘0‘+m;
    }
    if (s<10) {
        s=‘0‘+s;
    }
    
    return Y + M + D + ‘ ‘+ h + ‘:‘ + m + ‘:‘+ s; ////2014-06-18 10:33:24
}

//nonce 生成八位随机数(含有大小写字母和数字)
const getNonce = () => {
    return generateMixed(8);
}

const generateMixed = (n) => {
    // arr = [‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘];
    var chars = [‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘,
        ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘,
        ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘
    ];
    var res = "";
    for (var i = 0; i < n; i++) {
        var id = Math.ceil(Math.random() * 35);
        res += chars[id];
    }
    return res;
}

 参考:https://zhuanlan.zhihu.com/p/101333398

js 时间戳与日期之间的转换 随机字符串

标签:tno   lan   字符串   时间   logs   target   log   targe   字符   

原文地址:https://www.cnblogs.com/2186009311CFF/p/14253131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!