html页面获取当前时间
时间: 2023-07-06 09:31:41 浏览: 46
在 HTML 页面中获取当前时间可以使用 JavaScript 的 `Date` 对象来实现。以下是一个简单的例子:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>获取当前时间</title>
</head>
<body>
<p>当前时间是:</p>
<p id="time"></p>
<script>
// 创建 Date 对象,获取当前时间
const now = new Date();
// 将当前时间格式化为字符串,例如:2022年01月01日 12:34:56
const formattedTime = now.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit' });
// 将当前时间显示在页面上
document.getElementById('time').innerHTML = formattedTime;
</script>
</body>
</html>
```
在上面的例子中,我们使用 `new Date()` 创建了一个 `Date` 对象来获取当前时间,然后使用 `toLocaleString()` 方法将其格式化为字符串,并将其显示在页面上。你可以根据自己的需求,自定义时间的格式化方式。