用JS实现页面显示动态时钟

本文介绍如何利用JavaScript在网页上实现一个实时更新的动态时钟,展示当前的日期和时间,为用户提供即时的时间信息。

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

<body>
<script>
function tick() {
var ye,mo,da; //年月日
var hours, minutes, seconds, xfile; //时分秒,PM/AM
var intHours, intMinutes, intSeconds;
var today;
today = new Date();//创建一个时间对象
//ye = today.getFullYear();//获取完整的4位年份 ,跟下一句代码是一样的
ye=(today.getYear() < 1900) ? (1900 + today.getYear()) : today.getYear();
//Firefox和Chrome的getYear()返回的是“当前年份-1900”的值,而IE却当Year>=2000,直接把减去的1900又加回来了。

mo = today.getMonth()+1;//月份是从0开始,即0是1月
if (mo < 10) {
mo = "0"+mo;
}
da = today.getDate();//取出当前日期
if (da < 10) {
da = "0"+da;
}
intHours = today.getHours();//获取当前的小时数,存放在intHours里边
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();
if (intHours == 0) {
hours = "12:";
xfile = "AM";//午夜
} else if (intHours < 12) {
hours = intHours+":";
xfile = "AM";//上午
} else if (intHours ==
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值