<!DOCTYPE> <html> <head> <title>獲取當前時間</title> <meta charset="UTF-8"> <script type="text/javascript"> function showtime(){ var hour,second,minute,year,month,date; var strDate; var today=new Date(); var n_day = today.getDay(); switch(n_day) { case 0:{ strDate ="星期日";}break; case 1:{ strDate ="星期一";}break; case 2:{ strDate ="星期二";}break; case 3:{ strDate ="星期三";}break; case 4:{ strDate ="星期四";}break; case 5:{ strDate ="星期五";}break; case 6:{ strDate ="星期六";}break; } year = today.getFullYear(); //年 month = today.getMonth()+1; //月 date =today.getDate(); //日 hour = today.getHours();//時 minute =today.getMinutes();//分 second = today.getSeconds(); //秒 if(eval(hour)<10){hour="0"+hour;} if(eval(minute)<10){minute="0"+minute;} if(eval(second)<10){second="0"+second;} document.getElementById('time').innerHTML=" "+year + "年" + month + "月" + date + "日 " +strDate+""+hour+"時"+minute+"分"+second+"秒"; //顯示時間 setTimeout("showtime();", 1000); //設定函數自動執行時間爲 1000ms(1 s) } </script> </head> <body onload="showtime();"> <div id=time style="font-size: 24px; height 20px; color: highlight; background-color: #ffddaa;"></div> </body> </html>