动态显示日期
window.onload=setInterval(datestr,1000); function datestr(){ var date = new Date(); var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; var datestr = "当前日期:"+date.getFullYear()+"年"+((Number(date.getMonth()+1)<10 ? ("0"+(date.getMonth()+1)):(date.getMonth()+1)))+"月"+(Number(date.getDate()) <10 ? "0"+date.getDate():date.getDate())+"日 "+weekday[date.getDay()]+" "+((Number(date.getHours()+1)<10?("0"+(date.getHours()+1)):(date.getHours()+1)))+"时"+((Number(date.getMinutes()+1)<10?("0"+(date.getMinutes()+1)):(date.getMinutes()+1)))+"分"+((Number(date.getSeconds()+1)<10?("0"+(date.getSeconds()+1)):(date.getSeconds()+1)))+"秒"; //方法二:使用switch-case语句 /*switch(date.getDay()){ case 0: datestr += " 星期日"; break; case 1: datestr += " 星期一"; break; case 2: datestr += " 星期二"; break; case 3: datestr += " 星期三"; break; case 4: datestr += " 星期四"; break; case 5: datestr += " 星期五"; break; case 6: datestr += " 星期六"; break; }*/ var adv=document.getElementById("dateshow"); adv.innerHTML=datestr; }
效果: