首页 > 代码库 > 展示当前的时间

展示当前的时间


window.onload = function (){ var odiv1 = document.getElementById("div1"); function getTime(){ var myDate = new Date(); var myYear = myDate.getFullYear(), myMonth = myDate.getMonth() + 1, mynowDate = myDate.getDate(), myDay = myDate.getDay(), myHour = myDate.getHours(), myMinutes = myDate.getMinutes(), mySeconds = myDate.getSeconds(); myHour = newnum(myHour); myMinutes = newnum(myMinutes); mySeconds = newnum(mySeconds); myDay = changeDay(myDay); odiv1.innerHTML = myYear + "年" + myMonth + "月" + mynowDate + "日" + "星期" + myDay + "<br>" +myHour + ":" + myMinutes + ":" + mySeconds ; } function newnum(obj){ if(obj < 10) { obj = "0" + obj; } return obj; } function changeDay(obj){ switch (obj){ case 1: obj = "一" ; break; case 2: obj = "二" ; break; case 3: obj = "三" ; break; case 4: obj = "四" ; break; case 5: obj = "五" ; break; case 6: obj = "六" ; break; case 0: obj = "日" ; break; } return obj; } getTime(); var time = setInterval(getTime,1000); }

以上为js部分

 

页面html部分如下

<!DOCTYPE html><html>    <head>        <title>当前时间</title>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        <style type="text/css">            #div1 { font-size: 20px; font-weight: bold;}        </style>    </head>    <body>        <div id="div1" align="center"></div>    </body></html>

 

展示当前的时间