• HTML代码
1 <html>
2 <head>
3     <title>时间</title>
4     <meta charset="utf-8">
5 </head>
6 <body onload="getTime()">
7     <span  ></span> 
8 </body>
9 </html>
  • js代码
//时间
function getTime(){
    var ndate = new Date();
    var nyear = ndate.getFullYear();
    var nmonth = ndate.getMonth()+1;
    var nday = ndate.getDate(); 
    var nhour = ndate.getHours();
    var nminute = ndate.getMinutes();
    var nsecond = ndate.getSeconds();
    // 如果分钟或小时的值小于10,则在其值前加0,比如如果时间是下午3点20分9秒的话,则显示15:20:09
    nmonth=checkTime(nmonth);
    nday=checkTime(nday);
    nminute=checkTime(nminute);   
    nsecond=checkTime(nsecond);    
    document.getElementById('nowDateTimeSpan').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond;
    document.getElementById('nowDateTimeSpan1').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond;
    document.getElementById('nowDateTimeSpan2').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond;
    document.getElementById('nowDateTimeSpan3').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond;
    document.getElementById('nowDateTimeSpan4').innerHTML=nyear+"-"+nmonth+"-"+nday+" "+nhour+":"+nminute+":"+nsecond;   
    //setTimeout('getTime()',1000);//每一秒中重新加载startTime()方法
    function checkTime(i){   
        if (i<10){
            i="0" + i;
        }   
        return i;
    }
}

 

相关文章:

  • 2021-10-26
  • 2021-07-06
  • 2022-12-23
  • 2021-11-07
  • 2021-07-07
  • 2021-12-04
  • 2021-11-13
  • 2021-09-30
猜你喜欢
  • 2021-09-01
  • 2021-12-05
  • 2021-11-12
  • 2022-12-23
  • 2021-11-17
相关资源
相似解决方案