在HTML中使用JavaScript实时显示当前日期和时间(结尾完整例程)

在Web开发中,经常需要在网页上显示当前的日期和时间。HTML本身并不具备这样的动态功能,但我们可以借助JavaScript来实现。JavaScript是一种常用的前端脚本语言,它可以轻松地获取系统时间,并将其插入到HTML元素中。

下面是一个简单的示例,演示如何在HTML中使用JavaScript实时显示当前的日期和时间:

首先,我们创建一个HTML文件,并在其中添加一个用于显示日期和时间的<p>元素。给它一个唯一的id,这样我们就可以通过JavaScript找到它并更新其内容。

html 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>实时显示当前日期和时间</title>  
</head>  
<body>  
    <p id="currentDateTime">加载中...</p>  
  
    <!-- 引入JavaScript代码 -->  
    <script src="display-datetime.js"></script>  
</body>  
</html>

接下来,我们创建一个名为display-datetime.js的JavaScript文件,并编写用于获取和格式化当前日期时间的函数。

javascript 复制代码
// 显示当前日期时间的函数  
function showCurrentDateTime() {  
    const now = new Date();  
    const year = now.getFullYear();  
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始,所以+1  
    const day = String(now.getDate()).padStart(2, '0');  
    const hours = String(now.getHours()).padStart(2, '0');  
    const minutes = String(now.getMinutes()).padStart(2, '0');  
    const seconds = String(now.getSeconds()).padStart(2, '0');  
    const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;  
    document.getElementById('currentDateTime').textContent = formattedDateTime;  
}  
  
// 页面加载完成后立即显示当前日期时间  
window.onload = showCurrentDateTime;  
  
// 每秒更新一次日期时间显示  
setInterval(showCurrentDateTime, 1000);

在JavaScript文件中,我们定义了一个showCurrentDateTime函数,它使用Date对象获取当前的日期和时间,并将其格式化为YYYY-MM-DD HH:MM:SS的形式。然后,我们通过document.getElementById找到具有指定id的HTML元素,并使用textContent属性设置其内容为格式化后的日期时间字符串。

为了使页面加载完成后立即显示当前的日期时间,我们使用了window.onload事件。此外,我们还使用setInterval函数每秒调用一次showCurrentDateTime函数,以便实时更新显示的日期时间。

最后,将JavaScript文件与HTML文件关联起来。在HTML文件的<body>标签底部,使用<script>标签引入JavaScript文件。

现在,当您打开这个HTML页面时,它将实时显示当前的日期和时间,并且每秒都会自动更新。

完整例子程序

html 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>显示当前日期和时间</title>  
</head>  
<body>  
    <p id="currentDateTime">加载中...</p>  
  
    <script>  
        function showCurrentDateTime() {  
            const now = new Date();  
            const year = now.getFullYear();  
            const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的,所以要+1  
            const day = String(now.getDate()).padStart(2, '0');  
            const hours = String(now.getHours()).padStart(2, '0');  
            const minutes = String(now.getMinutes()).padStart(2, '0');  
            const seconds = String(now.getSeconds()).padStart(2, '0');  
            const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;  
            document.getElementById('currentDateTime').textContent = formattedDateTime;  
        }  
  
        // 初始显示当前日期和时间  
        showCurrentDateTime();  
  
        // 每秒更新一次日期和时间  
        setInterval(showCurrentDateTime, 1000);  
    </script>  
</body>  
</html>
相关推荐
Tiffany_Ho几秒前
【TypeScript】知识点梳理(三)
前端·typescript
安冬的码畜日常1 小时前
【D3.js in Action 3 精译_029】3.5 给 D3 条形图加注图表标签(上)
开发语言·前端·javascript·信息可视化·数据可视化·d3.js
太阳花ˉ1 小时前
html+css+js实现step进度条效果
javascript·css·html
小白学习日记2 小时前
【复习】HTML常用标签<table>
前端·html
john_hjy2 小时前
11. 异步编程
运维·服务器·javascript
风清扬_jd2 小时前
Chromium 中JavaScript Fetch API接口c++代码实现(二)
javascript·c++·chrome
丁总学Java2 小时前
微信小程序-npm支持-如何使用npm包
前端·微信小程序·npm·node.js
yanlele2 小时前
前瞻 - 盘点 ES2025 已经定稿的语法规范
前端·javascript·代码规范
It'sMyGo3 小时前
Javascript数组研究09_Array.prototype[Symbol.unscopables]
开发语言·javascript·原型模式