纯JS实现动态时间显示代码

本文介绍使用JavaScript在网页上实时显示当前日期时间的方法,并展示如何同时显示星期几。提供了多种实现方式,包括直接使用HTML与JavaScript,利用定时器刷新显示内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、

JavaScript获取当前日期时间同时显示星期几,具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript">
function currentTime(){
  var d=new Date(),str='';
  str+=d.getFullYear()+'年';
  str+=d.getMonth() + 1+'月';
  str+=d.getDate()+'日';
  str+=d.getHours()+'时';
  str+=d.getMinutes()+'分';
  str+= d.getSeconds()+'秒';
  return str;
}
setInterval(function(){$('#time').html(currentTime)},1000);
</script>
</head>
<body>
<div id="time"></div>
</body>
</html>

二、

在网页上及时动态显示当前的日期时间并显示星期的做法:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function showTime(){
  var show_day=new Array('星期一','星期二','星期三','星期四','星期五','星期六','星期日');
  var time=new Date();
  var year=time.getYear();
  var month=time.getMonth();
  var date=time.getDate();
  var day=time.getDay();
  var hour=time.getHours();
  var minutes=time.getMinutes();
  var second=time.getSeconds();
  month<10?month='0'+month:month;
  month=month+1;
  hour<10?hour='0'+hour:hour;
  minutes<10?minutes='0'+minutes:minutes;
  second<10?second='0'+second:second;
  var now_time='当前时间:'+year+'年'+month+'月'+date+'日'+' '+show_day[day-1]+' '+hour+':'+minutes+':'+second;
  document.getElementById('showtime').innerHTML=now_time;
  setTimeout("showTime();",1000); //动态调取时间

}


三、
  1. <span style="font-size:18px;">
  2. var datelocalweek=new Array("星期日""星期一""星期二","星期三","星期四""星期五","星期六");  
  3. var datelocalnow=new Date();   
  4. var datelocalyear=datelocalnow.getFullYear();   
  5. var datelocalmonth=(datelocalmonth="0"+(datelocalnow.getMonth()+1)).substr(datelocalmonth.length-2,2);   
  6. var datelocalday=(datelocalday="0"+datelocalnow.getDate()).substr(datelocalday.length-2,2);   
  7. var datelocalweekday=datelocalweek[datelocalnow.getDay()];   
  8. document.write("<font color=#fc7a05>北京时间</font><font color=#666666>: "+datelocalyear+"年"+datelocalmonth+"月"+datelocalday+"日"+"  "+datelocalnow.getHours()+":"+datelocalnow.getMinutes()+":"+datelocalnow.getSeconds()+" "+datelocalweekday+"</font>"); </span>  


解释下:

var datelocalnow=new Date();    建立一个时间类

var datelocalyear=datelocalnow.getFullYear();   获取年份

var datelocalmonth=(datelocalmonth="0"+(datelocalnow.getMonth()+1)).substr(datelocalmonth.length-2,2);   获取月份

var datelocalday=(datelocalday="0"+datelocalnow.getDate()).substr(datelocalday.length-2,2);  获取日期

var datelocalweekday=datelocalweek[datelocalnow.getDay()]; 获取星期

datelocalnow.getHours()  获取小时

datelocalnow.getMinutes() 获取分钟

datelocalnow.getSeconds() 获取秒


  1. /**       
  2.  * 对Date的扩展,将 Date 转化为指定格式的String       
  3.  * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符       
  4.  * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)       
  5.  * eg:       
  6.  * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423       
  7.  * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04       
  8.  * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04       
  9.  * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04       
  10.  * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18       
  11.  */    

四、

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
 <script language="javascript">
     var t = null;
    t = setTimeout(time,1000);//开始执行
    function time()
    {
       clearTimeout(t);//清除定时器
       dt = new Date();
       var h=dt.getHours();
       var m=dt.getMinutes();
       var s=dt.getSeconds();
       document.getElementById("timeShow").innerHTML =  "现在的时间为:"+h+"时"+m+"分"+s+"秒";
       t = setTimeout(time,1000); //设定定时器,循环执行            
    }
  </script>
</head><body>
<label id="timeShow"></lable>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值