首页 > 代码库 > js获取当前日期时间格式为“yyyy-MM-dd HH:MM:SS”

js获取当前日期时间格式为“yyyy-MM-dd HH:MM:SS”

废话不多说,直接上代码!

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
</head>

<body>
    <p id="demo">单击按钮。</p>
    <button onclick="getNowFormatDate()">我点点点</button>
    <div id="da"></div>
    <script>
    function getNowFormatDate() {   
        var date = new Date();   
        var seperator1 = "-";   
        var seperator2 = ":";   
        var month = date.getMonth() + 1;   
        var strDate = date.getDate();   
        if (month >= 1 && month <= 9) {       
            month = "0" + month;   
        }   
        if (strDate >= 0 && strDate <= 9) {       
            strDate = "0" + strDate;   
        }   
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();   
        var t = document.getElementById("da");
        t.innerHTML = currentdate
        console.log(currentdate);
    }
    </script>
</body>

</html>

js获取当前日期时间格式为“yyyy-MM-dd HH:MM:SS”