javascript 以“年-月-日 时:分:秒”格式显示当前时间

/**
 * Created by shgbit on 2015/1/9.
*js代码
*/ function showNow(){

    var t=new Date();
    var month=t.getMonth()+1<10? '0'+(t.getMonth()+1):t.getMonth()+1;
    var date=t.getDate()<10? '0'+t.getDate():t.getDate();
    var hour=t.getHours()<10 ? '0'+t.getHours():t.getHours();
    var minutes=t.getMinutes()<10 ? '0'+t.getMinutes():t.getMinutes();
    var seconds=t.getSeconds()<10 ? '0'+t.getSeconds():t.getSeconds();
    return t.getFullYear()+'-'+ month+'-'+date +' '+hour +':'+ minutes+':'+ seconds;

}

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <script type="text/javascript" src="https://files.cnblogs.com/greenteaone/tddd.js" ></script>
    <title>显示当前时间</title>
</head>
<body>
 <label id="test">hh</label>
 <script type="text/javascript">
    window.onload=function(){
        test();
        setInterval(test,1000)
    };
     function test(){
         document.getElementById('test').innerText=showNow();
     }
 </script>
</body>
</html>

 

posted @ 2015-01-09 11:53  绿茶叶  阅读(504)  评论(0编辑  收藏  举报