Home > Web Front-end > JS Tutorial > body text

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

高洛峰
Release: 2017-01-07 16:27:09
Original
2595 people have browsed it

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>
Copy after login


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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!