企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 1.Math ~~~ Math.ceil() Math.floor() Math.round() Math.random() Math.max() Math.min ~~~ # 2.Date ~~~ //获取当前时间 var oDate = new Date(); var year = oDate.getFullYear(); var month = oDate.getMonth(); var date = oDate.getDate(); var hour = oDate.getHours(); var minute = oDate.getMinutes(); var second = oDate.getSeconds(); var mill = oDate.getTime(); //月份从0开始 var month = oDate.getMonth()+1; //星期从0开始,0表示星期天 //0,1,2,3,4,5,6 var day = oDate.getDay(); ~~~ ## 例子:实现一个数码时钟 [在线演示](https://html5_of_the_buddha_department.gitee.io/clock/) ## 思路 - 获取系统时间 1. Date对象 2. getHours,getMinutes,getSeconds - 显示系统时间 1. 字符串连接 2. 小于10补零 - 使用定时器 setInterval() ~~~ //HTML <div id="clock"> <img src="images/0.png" alt=""> <img src="images/0.png" alt=""> <img src="images/0.png" alt=""> <img src="images/0.png" alt=""> <img src="images/0.png" alt=""> <img src="images/0.png" alt=""> </div> ~~~ ~~~ showTime(); setInterval(showTime,1000) function showTime() { var d = new Date(); var hour = d.getHours(); var minute = d.getMinutes(); var second = d.getSeconds(); //1.小10补零 function add(time) { if (time < 10) { return "0" + time } else { return time + ""; } } var allTime = add(hour) + add(minute) + add(second); //2.分割字符串显示系统时间 var arr = allTime.split(""); console.log(arr); var imgs = document.getElementsByTagName("img"); for (let i = 0; i < imgs.length; i++) { imgs[i].src = "images/" + arr[i] + ".png" } } ~~~ [码云地址](https://gitee.com/html5_of_the_buddha_department/clock)