JavaScript获取当前时间

<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>前端ABC-qdabc.cn</title>
        <link rel="stylesheet" href="">
        <style type="text/css" media="screen">
                img{
                        width: 450px;
                        height: 500px;
                }
        </style>
</head>
<script type="text/javascript" >
        window.onload = function(){
                var str;
                function fnTime(){
                        var time = new Date();
                        var tyear = time.getFullYear();
                        var tmonth = time.getMonth() + 1;
                        var tdate = time.getDate();
                        var tday = time.getDay();
                        var tHours = time.getHours();
                        var tMin = time.getMinutes();
                        var tSec = time.getSeconds();

                        if(tday == 0){
                                tday = "星期日";
                        }else if(tday == 1){
                                tday = "星期一";        
                        }else if(tday == 2){
                                tday = "星期二";        
                        }else if(tday == 3){
                                tday = "星期三";        
                        }else if(tday == 4){
                                tday = "星期四";        
                        }else if(tday == 5){
                                tday = "星期五";        
                        }else if(tday == 6){
                                tday = "星期六";        
                        }

                        function num(n){
                                return n<10 ?  '0' + n : n;  //单位数的加零
                        }
                        str = tyear +'年'+ tmonth +'月'+ tdate +'日 '+ tday +'  '+ tHours +' : '+ tMin +' : '+ num(tSec);
                        document.body.innerHTML = str;
                }
                fnTime();
                setInterval(fnTime, 1000);              
        }
</script>
<body>    
</body>
</html>

欢迎分享本文,转载请保留出处:前端ABC » JavaScript获取当前时间

赞 (2)
分享到:更多 ()

发表评论 0