In: Computer Science
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59.
Write a driver program to test your class and call it testTime.. Make sure to test all the methods
Use JavaScript language
Hi,
Please find the below output with code:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Class Method</h2>
<p>How to define and use a Class method.</p>
<h1> Demo For 24 Hours Time Format</h1>
=======================================================
<p id="demo1"></p>
=======================================================
<p id="demo2"></p>
======================================================
<p id="demo3"></p>
=======================================================
<h1> Demo For 12 Hours Time Format</h1>
======================================================
<p id="demo4"></p>
=======================================================
<p id="demo5"></p>
======================================================
<p id="demo6"></p>
=======================================================
<script>
classObject = [];
//The hour value ranges from 0 to 23
//0 to 11 represents a time before noon
//minute value ranges from 0 to 59
//Class Time
class Time {
constructor() {
this.hour = 0;
this.minte = 0;
this.isAM = false;
classObject = this;
}
//Is Valid Method
isValid(hour, minute) {
this.hour = hour;
this.minte = minute;
}
//convert time into 12 hour format
getTime12(hour, minute) {
var xx = "";
//Check am and pm
if (hour >= 1 && hour < 12) {
xx = " am";
}
else {
xx = " pm";
}
//Convert 24 hours time into 12 hours
if (hour == 0) {
hour = 12;
}
else if (hour >= 12) {
hour = hour - 12;
}
//If minute digit is 1 i.e. < 9 then prepend 0 in the minute
if (minute <= 9) {
minute = "0" + minute;
}
//Return contactinated hours and minute
return hour + ":" + minute + xx;
}
//convert time into 24 hour format
getTime24(hour, minute) {
//If hour digit is single ditit the and prepend 0
if (hour < 10) {
hour = "0" + hour;
}
//If minute digit is single digit then prepend 0
if (minute < 10) {
minute = "0" + minute;
}
//Return contactinated hours and minute
return hour + "" + minute;
}
//Set time method
setTime(hour, minute, isAM) {
this.hour = hour;
this.minte = minute;
//set am and pm
if (hour >= 1 && hour < 12) {
this.isAM = true;
}
else {
this.isAM = false;
}
}
}
//instantiate Class
let myTime = new Time();
//declare variable
var formateDateTime;
//Check here 24 hours times
formateDateTime = myTime.getTime24(7, 25);
document.getElementById("demo1").innerHTML = "Input is - 7,25 for the 24 Hour Format is: Output is -" + formateDateTime + " ";
formateDateTime = myTime.getTime24(0, 5);
document.getElementById("demo2").innerHTML = "Input is - 0,5 for the 24 Hour Format is: Output is " + formateDateTime + " ";
formateDateTime = myTime.getTime24(15, 30);
document.getElementById("demo3").innerHTML = "Input is - 15,30 for the 24 Hour Format is: Output is " + formateDateTime + " ";
//Check here 12 hours times
formateDateTime = myTime.getTime12(7, 25);
document.getElementById("demo4").innerHTML = "Input is - 7,25 for the 12 Hour Format is: Output is -" + formateDateTime + " ";
formateDateTime = myTime.getTime12(0, 5);
document.getElementById("demo5").innerHTML = "Input is - 0,5 for the 12 Hour Format is: Output is " + formateDateTime + " ";
formateDateTime = myTime.getTime12(15, 30);
document.getElementById("demo6").innerHTML = "Input is - 15,30 for the 12 Hour Format is: Output is " + formateDateTime + " ";
</script>
</body>
</html>
Thanks.