代码改变世界

ASP.NET JavaScript显示动态时间

2011-03-04 00:13  张瀚文zhw  阅读(1721)  评论(1编辑  收藏  举报
View Code
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
2
3  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title>ASP.NET JavaScript显示动态时间 </title>
8 <%--JS代码--%>
9 <script>
10 function tick() {
11 var hours, minutes, seconds, xfile;
12 var intHours, intMinutes, intSeconds;
13 var today, theday;
14 today = new Date();
15 function initArray(){
16 this.length=initArray.arguments.length
17 for(var i=0;i<this.length;i++)
18 this[i+1]=initArray.arguments[i] }
19 var d=new initArray(
20 "星期日",
21 "星期一",
22 "星期二",
23 "星期三",
24 "星期四",
25 "星期五",
26 "星期六");
27 theday = today.getYear()+"" + [today.getMonth()+1]+"" +today.getDate()+ d[today.getDay()+1];
28 intHours = today.getHours();
29 intMinutes = today.getMinutes();
30 intSeconds = today.getSeconds();
31 if (intHours == 0) {
32 hours = "12:";
33 xfile = "午夜";
34 } else if (intHours < 12) {
35 hours = intHours+":";
36 xfile = "上午";
37 } else if (intHours == 12) {
38 hours = "12:";
39 xfile = "正午";
40 } else {
41 intHours = intHours - 12
42 hours = intHours + ":";
43 xfile = "下午";
44 }
45 if (intMinutes < 10) {
46 minutes = "0"+intMinutes+":";
47 } else {
48 minutes = intMinutes+":";
49 }
50 if (intSeconds < 10) {
51 seconds = "0"+intSeconds+" ";
52 } else {
53 seconds = intSeconds+" ";
54 }
55 timeString = theday+xfile+hours+minutes+seconds;
56 Clock.innerHTML = timeString;
57 window.setTimeout("tick();", 100);
58 }
59 window.onload = tick;
60 </script>
61 </head>
62 <body>
63 <%--前台显示--%>
64 <div id="Clock" align="center" style="font-size: 14px; color:#000000"></div>
65 </body>
66 </html>

在客户端通过JS在前台显示动态时间,精确到秒实时更新。显示年、月、日、星期、早、中、晚、小时、分钟、秒。