javascript实现页面的实时时钟显示示例
作者:weixin_42363997 时间:2024-04-10 10:49:07
时钟实现
实现这个时钟时间需要解决以下三个问题:
获得当前时间,并格式化
如何可以在页面中显示时间
让时间动起来
1、获得当前时间,并格式化
要获得当前时间,可以使用JavaSctipt的Date对象,默认构造函数会返回当前时间。存储日期为自 1970 年 1 月 1 日 00:00:00以来的毫秒数。
setXxx 这些方法用于设置时间和日期值
getXxx 这些方法用于获取时间和日期值
实现代码如下:
function getnow(){
//1、获得当前时间,格式化时间
var now=new Date();
var year=now.getFullYear();
var month=now.getMonth()+1;
if(month<10){
month="0"+month;
}
var date=now.getDate();
if(date<10){
date="0"+date;
}
var hour=now.getHours();
if(hour<10){
hour="0"+hour;
}
var minute=now.getMinutes();
if(minute<10){
minute="0"+minute;
}
var second=now.getSeconds();
if(second<10){
second="0"+second;
}
var nowstr=year+"年"+month+"月"+date+"日 "+hour+":"+minute+":"+second;
alert(nowstr);
}
在body标签中使用onLoad事件绑定这个函数,使页面一加载就运行这个时钟。
<body οnlοad="getnow()">
</body>
运行代码就可以看到如下效果:
2、如何可以在页面中显示时间
目的当然不是要通过弹出框的方式显示时间,而是让他在页面中显示,可以在页面中定义一个span标签显示时间,如何可以使用JavaScript操作标签的内容呢?这里就需要用到内置对象document了,它提供了一个getElementById方法,可以根据id获得标签对象,然后就可以修改这个对象了。现在doby中顶一个一个span对象,并定义id属性,值为nowspan,代码如下:
<body οnlοad="
<span id="nowspan" >
</span>
</body>
修改上面的javascript代码,获得这个对象并把格式化好的时间赋给它。
// alert(nowstr);
//2、显示时间 html
//获得标签对象
var nowspan=document.getElementById("nowspan");
nowspan.innerHTML=nowstr;
3、让时间动起来
要让时间动起来可以使用setTimeout("函数",毫秒数) 设置定时器:经过指定毫秒值后执行某个函数。实现代码如下:
//3、使时间动起来
setTimeout("getnow()", 1000);
完整代如下:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<script type="text/javascript">
function getnow(){
//1、获得当前时间,格式化时间
var now=new Date();
var year=now.getFullYear();
var month=now.getMonth()+1;
if(month<10){
month="0"+month;
}
var date=now.getDate();
if(date<10){
date="0"+date;
}
var hour=now.getHours();
if(hour<10){
hour="0"+hour;
}
var minute=now.getMinutes();
if(minute<10){
minute="0"+minute;
}
var second=now.getSeconds();
if(second<10){
second="0"+second;
}
var nowstr=year+"年"+month+"月"+date+"日 "+hour+":"+minute+":"+second;
//alert(nowstr);
//2、显示时间 html
//获得标签对象
var nowspan=document.getElementById("nowspan");
nowspan.innerHTML=nowstr;
//3、使时间动起来
setTimeout("getnow()", 1000);
}
</script>
<body onLoad="getnow()">
<span id="nowspan" />
</body>
</html>
运行代码效果:
来源:https://blog.csdn.net/weixin_42363997/article/details/81433417
标签:javascript,页面,实时,时钟
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微信小程序获取当前位置的详细步骤
2024-04-08 10:52:09
![](https://img.aspxhome.com/file/2023/8/136988_0s.jpg)
python使用mailbox打印电子邮件的方法
2023-09-05 10:15:30
算法系列15天速成 第二天 七大经典排序【中】
2022-01-10 10:10:25
![](https://img.aspxhome.com/file/2023/7/132437_0s.gif)
python使用pil进行图像处理(等比例压缩、裁剪)实例代码
2022-12-22 16:50:09
Go 语言入门之Go 计时器介绍
2024-05-25 15:16:04
Microsoft SQL Server数据库开发问题详解
2009-10-23 14:16:00
浅谈Python中的数据类型
2022-11-29 06:19:22
图文详解Python中最神秘的一个魔法函数
2022-03-25 05:44:24
![](https://img.aspxhome.com/file/2023/7/104937_0s.jpg)
Python 实现简单的电话本功能
2021-11-16 09:47:58
Go语言实现一个简单的并发聊天室的项目实战
2024-02-15 03:40:33
Python 中Operator模块的使用
2021-04-02 05:30:34
![](https://img.aspxhome.com/file/2023/5/83845_0s.png)
Vue Cli3 创建项目的方法步骤
2024-05-21 10:16:53
![](https://img.aspxhome.com/file/2023/8/125288_0s.png)
python输出第n个默尼森数的实现示例
2022-08-12 21:07:25
15个应该掌握的Jupyter Notebook使用技巧(小结)
2023-12-14 15:25:05
![](https://img.aspxhome.com/file/2023/9/124379_0s.png)
JS获得选取checkbox整行数据的方法
2024-02-26 21:10:33
Linux中安装Python的交互式解释器IPython的教程
2023-09-14 05:50:06
php安全攻防利用文件上传漏洞与绕过技巧详解
2023-07-23 12:08:06
Python 连接 MySQL 的几种方法
2024-01-16 21:51:09
![](https://img.aspxhome.com/file/2023/6/80616_0s.jpg)
Python远程桌面协议RDPY安装使用介绍
2023-11-06 05:37:23
django2+uwsgi+nginx上线部署到服务器Ubuntu16.04
2022-01-15 17:03:09
![](https://img.aspxhome.com/file/2023/8/97678_0s.png)