<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> div{width: 300px;height: 100px;margin: 50px auto;border:1px solid #888;line-height: 100px;text-align: center;} </style> </head> <body> <div id="box"> </div> <script type="text/javascript"> var a=document.getElementById('box'); function showTime(){ var date = new Date();//创建日起对象的实例 //获取年份 var year = date.getFullYear(); var month = date.getMonth()+1; var day = date.getDate(); if (month<10) { month= "0"+month; } if (day<10) { day="0"+day; } //获取时分秒 var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); if (hour<10) { hour="0"+hour; } if (minute<10) { minute="0"+minute; } if (second<10) { second="0"+second; } //获取星期 var week = date.getDay(); //输出 var time = year + "年" + month + "月" + day + "日" +" "+ hour + ":" + minute + ":" + second +" "+ "星期" + week; a.innerHTML=time; } showTime(); setInterval(showTime,1000); </script> </body> </html>
文章来源: https://blog.csdn.net/weixin_44321299/article/details/86021084