am pm time html javascript

Solutions on MaxInterview for am pm time html javascript by the best coders in the world

showing results for - "am pm time html javascript"
Isabelle
29 Aug 2016
1<script>
2function startTime() {
3    var today=new Date();
4    var h=today.getHours();
5    var m=today.getMinutes();
6    var s=today.getSeconds();
7    var ampm = "";
8    m = checkTime(m);
9    s = checkTime(s);
10
11    if (h > 12) {
12        h = h - 12;
13        ampm = " PM";
14    } else if (h == 12){
15        h = 12;
16        ampm = " PM";
17    } else if (h < 12){
18        ampm = " AM";
19    } else {
20        ampm = "PM";
21    };
22
23    if(h==0) {
24    h=12;
25    }
26
27document.getElementById('Time').innerHTML = h+":"+m+":"+s+ampm;
28var t = setTimeout(function(){startTime()},500);
29}
30startTime();
31
32function checkTime(i) {
33    if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
34    return i;
35}
36</script>
37
38<body onload='startTime()'>
39  <div id='Time'></div> <!-- use some css with #Time -->
40</body>