We would like to know how to format a date using padding.
<!-- ww w . ja v a 2 s . c om-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
function pad(num) { return ("0"+num).slice(-2); }
function getDisplayDate() {
var date = new Date();
return date.getFullYear()+
"-"+pad(date.getMonth()+1)+
"-"+pad(date.getDate())+
" "+pad(date.getHours())+
":"+pad(date.getMinutes())+
":"+pad(date.getSeconds());
}
setInterval(function() {
document.getElementById("time").innerHTML=getDisplayDate();
},500);
</script>
</head>
<body>
<span id="time"></span>
</body>
</html>
The code above is rendered as follows: