html中获取当前时间
时间: 2023-05-26 09:02:45 浏览: 70
在HTML中无法获取当前时间,需要使用JavaScript来实现。以下是示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>获取当前时间</title>
<script type="text/javascript">
function showTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
document.getElementById('time').innerHTML = time;
setTimeout(showTime, 1000); // 每隔1秒钟更新时间
}
</script>
</head>
<body onload="showTime()">
<p>当前时间为:<span id="time"></span></p>
</body>
</html>
```
在上面的代码中,使用JavaScript的Date对象获取当前时间,并将年、月、日、时、分、秒拼接成字符串,通过innerHTML属性赋值给id为"time"的元素。使用setTimeout函数每隔1秒钟更新时间。在页面加载完成后,调用showTime函数开始更新时间。最后在HTML页面中显示当前时间。