Home  >  Article  >  Web Front-end  >  JavaScript hour, minute, and second time code (automatic zero padding)

JavaScript hour, minute, and second time code (automatic zero padding)

高洛峰
高洛峰Original
2017-01-07 16:27:092636browse

JavaScript time code for hours, minutes and seconds. If the time is less than 10, add a zero.

<html> 
<head> 
<script type="text/javascript"> 
function startTime() 
{ 
var today=new Date() 
var h=today.getHours() 
var m=today.getMinutes() 
var s=today.getSeconds() 
//add a zero in front of numbers which<10 
h=checkTime(h) 
m=checkTime(m) 
s=checkTime(s) 
document.getElementById(&#39;wux&#39;).innerHTML=h+":"+m+":"+s 
t=setTimeout(&#39;startTime()&#39;,100) 
} 
//add a zero in front of numbers which<10 
function checkTime(i) 
{ 
if (i<10) 
{ 
i="0" + i 
} 
return i 
} 
</script> 
</head> 
<body onload="startTime()"> 
<div id="wux"> 
</div> 
</body> 
</html>


For more JavaScript hour, minute, and second time code (automatic zero padding) related articles, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn