html js写一个实时显示系统时间代码
时间: 2023-04-03 18:00:38 浏览: 110
你好,以下是代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>实时显示系统时间</title>
<meta charset="utf-8">
<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 timeStr = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
document.getElementById("time").innerHTML = timeStr;
}
setInterval("showTime()", 100);
</script>
</head>
<body>
<p>当前时间是:<span id="time"></span></p>
</body>
</html>
```
希望能对您有所帮助。
相关问题
php js动态显示系统时间,动态显示当前时间的JS代码
以下是一个使用 PHP 和 JavaScript 动态显示系统时间的例子:
```php
<?php
date_default_timezone_set('Asia/Shanghai'); // 设置时区
?>
<html>
<head>
<title>动态显示系统时间</title>
<script type="text/javascript">
function showTime() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
document.getElementById("time").innerHTML = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
setTimeout(showTime, 1000); // 每隔1秒更新时间
}
window.onload = function() {
showTime();
};
</script>
</head>
<body>
<p>当前时间:<span id="time"><?php echo date('Y-m-d H:i:s'); ?></span></p>
</body>
</html>
```
这个例子中,PHP 代码用于设置时区,并在页面中动态输出当前时间。JavaScript 代码用于每隔一秒更新时间,并将更新后的时间显示在页面上。
js写一份动态系统时钟显示
好的,以下是一份使用 JavaScript 实现动态系统时钟显示的代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>动态系统时钟</title>
<style>
#clock {
font-size: 2em;
text-align: center;
margin: 50px;
}
</style>
</head>
<body>
<div id="clock"></div>
<script>
function updateClock() {
var now = new Date(); // 获取当前时间
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
// 小时、分钟、秒钟如果小于10,前面补0
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
var time = hours + ':' + minutes + ':' + seconds; // 格式化时间字符串
document.getElementById('clock').innerHTML = time; // 更新时钟显示
setTimeout(updateClock, 1000); // 每隔1秒钟更新一次时钟
}
updateClock(); // 调用函数,启动时钟
</script>
</body>
</html>
```
这份代码会在页面上显示一个动态系统时钟,每隔1秒钟更新一次时间。你可以将代码复制到一个 HTML 文件中,然后在浏览器中打开该文件,就可以看到动态系统时钟的效果。