標籤:效果 cond 對象 img 年月日 time函數 round second image
<html> <head> <title>簡單時間顯示</title> <style> div{ height:60px; background-color:lightgray; } </style> <script> function showTime(){ var time=new Date();//建立時間對象,獲得目前時間 var hour,minute,second;//提取時分秒 hour=time.getHours(); minute=time.getMinutes(); second=time.getSeconds(); //分、秒小於10的時候,應該顯示為0* minute=checkTime(minute); second=checkTime(second); var year,month,day;//提取年月日,月從0開始,所以加1 year=time.getFullYear();//getYear()函數獲得的是現在的年份-1900 month=time.getMonth()+1; day=time.getDate(); var weekday; weekday=time.getDay();//星期weekday:weekday=0~6:星期日~星期六 var weekDays=new Array("日","一","二","三","四","五","六"); //寫入到html var x=document.getElementById("p-time"); var str=year+"年"+month+"月"+day+"日"+" "+"星期"+weekDays[day]+" "+hour+":"+minute+":"+second; x.innerHTML=str; setTimeout(‘showTime()‘,500);//每500毫秒調用一次showTime函數來重新整理。一定注意函數的大小寫 } function checkTime(x){ //這裡不能寫成var x: if(x<10){ x=‘0‘+x; } return x; } </script> </head> <body onload="showTime()"> <div> <p id="p-time"></p> </div> </body></html>
:
JS練習-簡單顯示時間