JS动态获取当前时间,并写到指定的区域
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <script type="text/javascript"> 6 function bodyLoad(){ 7 8 var dateTime=new Date(); 9 var hh=dateTime.getHours();//小时 10 var mm=dateTime.getMinutes();//分钟 11 var ss=dateTime.getSeconds();//秒钟 12 13 var yy=dateTime.getFullYear();//年份 14 var MM=dateTime.getMonth()+1; //月份-因为1月这个方法返回为0,所以加1 15 var dd=dateTime.getDate();//日数 16 17 var week=dateTime.getDay();//周(0~6,0表示星期日) 18 var days=[ "日 ", "一 ", "二 ", "三 ", "四 ", "五 ", "六 ",] 19 20 document.getElementById("date").innerHTML=yy+"年"+MM+"月"+dd+"日 "+"星期"+days[week] ; 21 document.getElementById("time").innerHTML=hh+"时"+mm+"分"+ss+"秒"; 22 23 setTimeout(bodyLoad,1000); 24 } 25 </script> 26 </head> 27 <body onload="bodyLoad()"> 28 <span id="date" ></span> 29 <span id="time" ></span> 30 </body> 31 </html>
另外一种。
1 <script type="text/javascript"> 2 3 function gettime() 4 { 5 var t = new Date(); 6 var year = t.getFullYear(); //年 7 var month = t.getMonth() + 1; //月 8 var day = t.getDate(); //日 9 var hours = t.getHours(); 10 var minutes = t.getMinutes(); 11 var seconds = t.getSeconds(); 12 var week = t.getDay(); 13 var weekArr = new Array('周日','周一','周二','周三','周四','周五','周六'); 14 week = weekArr[week]; 15 var show_str =""; 16 var show_str = year + "年"; 17 18 if(month < 10) 19 show_str += "0"; 20 21 show_str += month + "月"; 22 23 if(day < 10) 24 show_str += "0"; 25 26 show_str += day + "日 "; 27 show_str +=(hours > 12) ? "下午 ": "上午 "; 28 show_str += ((hours > 12) ? hours-12 : hours); 29 show_str += ((minutes <10) ? ":0" : ":") + minutes; 30 show_str += ((seconds <10) ? ":0" : ":") + seconds; 31 show_str += ' '+week; 32 $("#led_date").html(show_str); //jQuery 代码 33 timerID = setTimeout(function(){gettime()},1000); 34 } 35 </script>
by hacket