js页面实时显示时间

时间:2022-09-26 20:36:35

1.通过getMonth()实现获取月份,从0开始计数,需要+1;

2.通过getDay()实现获取星期天数,从0开始,0表示星期日;

3.通过getDate()获取日期。

4.setTimeout(time1,1000);   1秒

5.在div中显示<div id="inn"></div>,使用.innerHTML实现赋值;

  在使用<input id="inn" type="text" />,使用.value实现赋值;

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'MyJsp.jsp' starting page</title>
13 <script type="text/javascript"></script>
14
15 <style>
16 #inn{
17 border:1px solid #abcdef;
18 height:100px;
19 width:500px;
20 }
21 </style>
22 </head>
23 <body>
24 <div id="inn"></div>
25 <script language="JavaScript">
26 function time1(){
27 var inn=document.getElementById('inn');
28
29 var date = new Date();
30
31 var month = date.getMonth()+1;
32 var year=date.getFullYear();
33 var day=date.getDate();
34 var week = date.getDay();
35 var hour = date.getHours();
36 var min = date.getMinutes();
37 var sec = date.getSeconds();
38 var week1;
39 switch(week)
40 {
41 case 0: week1='星期日'; break;
42 case 1: week1='星期一'; break;
43 case 2: week1='星期二'; break;
44 case 3: week1='星期三'; break;
45 case 4: week1='星期四'; break;
46 case 5: week1='星期五'; break;
47 case 6: week1='星期六'; break;
48 }
49 inn.innerHTML=year+'年'+month+'月'+day+'日'+week1+' 时间为:'+hour+':'+min+':'+sec;
50 var innn=inn.innerHTML;
51 setTimeout(time1,1000);
52 }
53 time1();
54 </script>
55 </body>
56 </html>