js时间戳

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <script type="text/javascript">
    /*
                *一、时间转换时间戳

                */
    var date = new Date(); //时间对象
    var str = date.getTime(); //转换成时间戳
    console.log(str);
    /*
    *二、时间戳转换为时间

    */
    // 转换成形如 ‎2018‎年‎1‎月‎4‎日‎ ‎14‎:‎00‎:‎00 格式:
    function getDate(date) {
        var t = new Date(date).toLocaleString();
        return t;

    }
    console.log(getDate(date));
    //
    // 也很简单
    var strtime = '2014-04-23 18:55:49:123';

    var date = new Date(strtime.replace(/-/g, '/'));

    // 有三种方式获取,在后面会讲到三种方式的区别
    time1 = date.getTime();
    time2 = date.valueOf();
    time3 = Date.parse(date);
    console.log(time1)
    console.log(time2)
    console.log(time3)

    /* 
    三种获取的区别:
    第一、第二种:会精确到毫秒
    第三种:只能精确到秒,毫秒将用0来代替
    比如上面代码输出的结果(一眼就能看出区别):
    1398250549123
    1398250549123
    1398250549000 
    */
    //
    // 比如需要这样的格式 yyyy-MM-dd hh:mm:ss
    var date = new Date(1398250549490);
    Y = date.getFullYear() + '-';
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    D = date.getDate() + ' ';
    h = date.getHours() + ':';
    m = date.getMinutes() + ':';
    s = date.getSeconds(); 
    console.log(Y+M+D+h+m+s); 
    // 输出结果:2014-04-23 18:55:49
    </script>
</body>

</html>

效果图:

原链接:https://www.cnblogs.com/Donnnnnn/p/8257493.html

jquery时间戳转化

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>时间戳</title>
</head>

<body>
    <div id="demo"></div>
    <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    function timetrans(date) {
        var date = new Date(date), //如果date为13位不需要乘1000,反之则需要*1000
            Y = date.getFullYear(),
            M = date.getMonth() + 1,
            D = date.getDate(),
            h = date.getHours(),
            m = date.getMinutes(),
            s = date.getSeconds();
        return Y + "" + (M < 10 ? "0" + M : M) + "" + (D < 10 ? "0" + D : D) + "" + (h < 10 ? "0" + h : h) + "" + (m < 10 ? "0" + m : m) + "" + (s < 10 ? "0" + s : s) + ""
    }
    let time = 1542274800000;
    $("#demo").text(timetrans(time)) //2018年11月15日17时40分00秒
    </script>
</body>

</html>

 时间分割方法:

formDate(time) {
            let timeList = time.split('-');
            return `${timeList[0]}年${timeList[1]}月${timeList[2]}日`
        }

 es6写法时间戳

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>时间戳</title>
</head>

<body>
    <script>
        function formTime(timeNum, type) {
            let text = type
            let date = new Date(timeNum)
            let year = date.getFullYear()
            let month = String(date.getMonth() + 1).padStart(2, '0')
            let day = String(date.getDate()).padStart(2, '0')
            let hour = String(date.getHours()).padStart(2, '0')
            let min = String(date.getMinutes()).padStart(2, '0')
            let sec = String(date.getSeconds()).padStart(2, '0')
            if (text.includes('YYYY')) text = text.replace(/YYYY/ig, year)
            if (text.includes('MM')) text = text.replace(/MM/ig, month)
            if (text.includes('DD')) text = text.replace(/DD/ig, day)
            if (text.includes('hh')) text = text.replace(/hh/ig, hour)
            if (text.includes('mm')) text = text.replace(/mm/ig, min)
            if (text.includes('ss')) text = text.replace(/ss/ig, sec)
            return text
        }
        console.log('年月日时分秒', formTime(1582623480 * 1000, 'YYYY-MM-DD hh:mm:ss'))
        console.log('月日时分', formTime(1582623480 * 1000, 'MM-DD hh:mm:ss'))
    </script>

</body>

</html>

原文地址:https://www.cnblogs.com/huanghuali/p/9336549.html