1.方法checkTime的调用,如果当前分钟数小于10,前面加0
var today=new Date();
var minutes=today.getMinutes();
minutes=checkTime(minutes);
function checkTime(i){
if(i<10){
i="0"+i;
return i;
}
}
2.一个时钟
<!DOCTYPE html>
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function startTime(){
var today=new Date();
var hours=today.getHours();
var minutes=today.getMinutes();
var seconds=today.getSeconds();
minutes= checkTime(minutes);
seconds = checkTime(seconds);
document.getElementById("txt").innerHTML=hours+":"+minutes+":"+seconds;
document.getElementById("hour").innerHTML=hours;
document.getElementById("minutes").innerHTML=minutes;
document.getElementById("seconds").innerHTML=seconds;
t=setTimeout("startTime()",500);
}
function checkTime(i){
if(i<10)
(i="0"+i);
return i;
}
</script>
<style>
#timer{
height:40px;
width:100px;
background:#000000;
border-redius:10px;
}
#hour,#minutes,#seconds{
float:left;
margin-top:10px;
margin-left:10px;
background:#FFFFFF;
font-size:20px;
}
</style>
</head>
<body οnlοad="startTime()">
<div id="txt"></div>
<div id="timer">
<div id="hour"></div>
<div id="minutes"></div>
<div id="seconds"></div>
</div>
</body>
</html>