-
Notifications
You must be signed in to change notification settings - Fork 0
/
timedisplay.js
43 lines (42 loc) · 1.58 KB
/
timedisplay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//function that given the time return a string of hour:minutes
function stringHoursMinutes(time) {
//get the hours
var hours = time.getHours();
//get the minutes
if (parseInt(time.getMinutes()) < 10) {minutes = "0" + time.getMinutes();} else minutes = time.getMinutes();
//return the string
return hours + ":" + minutes;
}
//function that given the time return a string of hour:minutes:seconds
function stringHoursMinutesSeconds(time) {
//get the hours and minutes
var hoursminutes = stringHoursMinutes(time);
//get the seconds
if (parseInt(time.getSeconds()) < 10) { seconds = "0" + time.getSeconds(); } else seconds = time.getSeconds();
//return the string
return hoursminutes + ":" + seconds;
}
//function that given the time return a string of minutes:seconds
function stringMinutesSeconds(time) {
//get the minutes
if (parseInt(time.getMinutes()) < 10) {minutes = "0" + time.getMinutes();} else minutes = time.getMinutes();
//get the seconds
if (parseInt(time.getSeconds()) < 10) { seconds = "0" + time.getSeconds(); } else seconds = time.getSeconds();
//return the string
return minutes + ":" + seconds;
}
//define the function to update the time
function updateTime() {
//get the current date and extract the time
var dt = new Date();
//get the time in hours, minutes and seconds
var time = stringHoursMinutesSeconds(dt);
//update the time in the html
$('#time').text(time);
}
//execute a first time
updateTime();
//execute this every minute to update the time
setInterval(function(){
updateTime();
}, 1000);