代码
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>动态时钟</title></head><body><h1>你好,欢迎访问贵美商城!</h1><h2 id="time"></h2><script>//触发onload事件,定时器效果onload=function(){setInterval("getTime()",1000);}function getTime(){//实例化时间对象var date = new Date();//获取小时var hour = date.getHours();//获取分钟var minutes = date.getMinutes();//获取秒var seconds = date.getSeconds();//声明一个存储上下午的变量var amPm="";//将时间设成12小时制,并按照时间显示上午(AM)和下午(PM)if(hour>12){hour = hour-12;amPm="PM";}else{amPm="AM";}//判断如果分钟<10的是时候自动补0if(minutes<10){minutes="0"+minutes;}//判断如果秒数<10的时候自动补0if(seconds<10){seconds="0"+seconds;}//获取当前时间字符串var timeInfo = date.getFullYear()+"年"+ (date.getMonth()+1)+"月"+date.getDate()+"日"+"   "+hour+":"+minutes+":"+seconds;//星期中的某一天var week = date.getDay();//通过某一天来判断星期switch (week) {case 1:week="星期一";break;case 2:week="星期二";break;case 3:week="星期三";break;case 4:week="星期四";break;case 5:week="星期五";break;case 6:week="星期六";break;case 7:week="星期日";break;}//获取h标签var hNode=document.getElementById("time");//插入h标签hNode.innerHTML=timeInfo+"   "+amPm+"   "+week;}</script></body></html>