本篇文章给大家分享的是关于JS实现实时显示时间,有感兴趣的朋友可以看一看,有需要帮助的朋友也可以参考一下
在之前的项目中有做到需要在网页中实时显示当前时间,实现的效果如图所示:
1. html结构
<p class="col-xs-12 col-sm-6"> <i class="fa fa-plus-square-o" aria-hidden="true"></i> <span id="current-time"></span> </p>
2. js
/*实时显示时间*/ function showtime(){ var today,hour,second,minute,year,month,date; var strDate ; today=new Date(); var n_day = today.getDay(); switch (n_day){ case 0:{ strDate = "星期日" }break; case 1:{ strDate = "星期一" }break; case 2:{ strDate ="星期二" }break; case 3:{ strDate = "星期三" }break; case 4:{ strDate = "星期四" }break; case 5:{ strDate = "星期五" }break; case 6:{ strDate = "星期六" }break; case 7:{ strDate = "星期日" }break; } year = today.getFullYear(); month = today.getMonth()+1; date = today.getDate(); hour = today.getHours(); minute =today.getMinutes(); second = today.getSeconds(); if (month >= 1 && month <= 9) { month = "0" + month; } if (date >= 0 && date <= 9) { date = "0" + date; } if (hour >= 0 && hour <= 9) { hour = "0" + hour; } if (minute >= 0 && minute <= 9) { minute = "0" + minute; } if (second >= 0 && second <= 9) { second = "0" + second; } document.getElementById('current-time').innerHTML ="当前时间:"+ year + "年" + month + "月" + date + "日" +" "+ strDate +" " + hour + ":" + minute + ":" + second; //显示时间 setTimeout("showtime();", 1000); //设定函数自动执行时间为 1000 ms(1 s) }
相关推荐:
以上就是JS实现实时显示时间的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号