web显示当前时间
时间: 2023-09-03 14:05:54 浏览: 77
你可以使用JavaScript获取当前时间并将其显示在网页上。以下是一个示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>显示当前时间</title>
<script>
function showTime() {
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
document.getElementById("clock").innerHTML = strTime;
}
setInterval(showTime, 1000);
</script>
</head>
<body>
<h1>当前时间:</h1>
<div id="clock"></div>
</body>
</html>
```
这段代码使用了JavaScript的Date对象获取当前时间,并将其格式化为“小时:分钟:秒钟 AM/PM”的形式,然后将其显示在一个id为“clock”的div元素中。使用setInterval函数每隔一秒钟刷新一次显示时间的内容。