JavaScript—当前时间
效果:
代码:
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>倒计时js代码</title> <style> * { margin: 0; padding: 0; list-style: none; } body { font-size: 18px; text-align: center; } .time { height: 30px; padding: 200px; } </style> </head> <body> </div> <!--当前时间--> <div id="show"> </div> </body> <script>//当前时间 window.onload = function() { var show = document.getElementById("show"); setInterval(function() { var time = new Date(); // 程序计时的月从0开始取值后+1 var m = time.getMonth() + 1; var t = time.getFullYear() + "-" + m + "-" + time.getDate() + " " + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds(); show.innerHTML = t; }, 1000); }; </script> </html>
动态显示当前时间,带日期
效果:
代码:
//动态显示当前的时间 function GetTime(obj) { var myDate = new Date(); var Today = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; //获取当前年 var Years = myDate.getFullYear(); //获取当前月 var Months = myDate.getMonth() + 1; //获取当前日 var Dates = myDate.getDate(); //获取当前天是当前周的第几天 var Days = Today[myDate.getDay()]; //获取当前的时 var Hours = myDate.getHours(); //获取当前的分 var Minutes = myDate.getMinutes(); //获取当前的秒 var Seconds = myDate.getSeconds(); Months = Months < 10 ? "0" + Months : Months; Dates = Dates < 10 ? "0" + Dates : Dates; Hours = Hours < 10 ? "0" + Hours : Hours; Minutes = Minutes < 10 ? "0" + Minutes : Minutes; Seconds = Seconds < 10 ? "0" + Seconds : Seconds; return Years + "年" + Months + "月" + Dates + "日" + " " + Days + " " + Hours + ":" + Minutes + ":" + Seconds; } function showTitleInfo() { document.getElementById("dateInfo").innerHTML = GetTime(); setTimeout("showTitleInfo()", 1000); } showTitleInfo();