【2011.09.01】如何使用javaScript代码获取系统时间和日期?
1.JS代码部分
<script type="text/javascript"> function show(time) { var str ,colorhead,colorfoot; var year=time.getYear(); //获取年份 var month=time.getMonth()+1; //获取月份 if(month<10) month='0'+month; var day=time.getDate(); //获取日期 if(day<10) day='0'+day; var hours=time.getHours(); //获取小时 var minutes=time.getMinutes(); //获取分钟 if(minutes<10) minutes='0'+minutes; var seconds=time.getSeconds(); //获取秒数 if(seconds<10) seconds='0'+seconds; var week=time.getDay(); //获取星期 if(week==0) colorhead="<font color=\"#FF0000\">"; if (week > 0 && week < 6) colorhead="<font color=\"#FF0000\">"; if (week==6) colorhead="<font color=\"#FF0000\">"; if (week==0) week="星期日"; if (week==1) week="星期一"; if (week==2) week="星期二"; if (week==3) week="星期三"; if (week==4) week="星期四"; if (week==5) week="星期五"; if (week==6) week="星期六"; colorfoot="</font>" str = colorhead + year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + " " + week
+ colorfoot; return(str); } function tick() //设置每1秒更新一次 { var today; today = new Date(); document.getElementById("study").innerHTML =show(today); window.setTimeout("tick()", 1000); } tick(); </script>
2.HTML代码部分
<marquee direction="left" scrollamount="6" style="font-size: 16px;"> <font> <span style="font-size:14px;"> <span style="color:#000;">今天是:</span> <span id="study"></span> </span> </font> </marquee>