首页 > 代码库 > 展示当前的时间
展示当前的时间
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>
展示当前的时间
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。