Added Homework 7 Files
++ Added Homework 7 HTML/CSS/JS ++ Added Testing Files of various sorts.
This commit is contained in:
parent
44a7e9a792
commit
5328bed90d
9 changed files with 363 additions and 0 deletions
56
cs212/testing/digital-clock/script.js
Normal file
56
cs212/testing/digital-clock/script.js
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
function Time() {
|
||||
|
||||
// Creating object of the Date class
|
||||
var date = new Date();
|
||||
|
||||
// Get current hour
|
||||
var hour = date.getHours();
|
||||
// Get current minute
|
||||
var minute = date.getMinutes();
|
||||
// Get current second
|
||||
var second = date.getSeconds();
|
||||
|
||||
// Variable to store AM / PM
|
||||
var period = "";
|
||||
|
||||
// Assigning AM / PM according to current hour
|
||||
if (hour >= 12) {
|
||||
period = "PM";
|
||||
} else {
|
||||
period = "AM";
|
||||
}
|
||||
|
||||
// Converting the hour in 12-hour format
|
||||
if (hour == 0) {
|
||||
hour = 12;
|
||||
} else {
|
||||
if (hour > 12) {
|
||||
hour = hour - 12;
|
||||
}
|
||||
}
|
||||
|
||||
// Updating hour, minute, and second
|
||||
// if they are less than 10
|
||||
hour = update(hour);
|
||||
minute = update(minute);
|
||||
second = update(second);
|
||||
|
||||
// Adding time elements to the div
|
||||
document.getElementById("digital-clock").innerText = hour + " : " + minute + " : " + second + " " + period;
|
||||
|
||||
// Set Timer to 1 sec (1000 ms)
|
||||
setTimeout(Time, 1000);
|
||||
}
|
||||
|
||||
// Function to update time elements if they are less than 10
|
||||
// Append 0 before time elements if they are less than 10
|
||||
function update(t) {
|
||||
if (t < 10) {
|
||||
return "0" + t;
|
||||
}
|
||||
else {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
Time();
|
||||
Loading…
Add table
Add a link
Reference in a new issue