The JavaScript Code to print “Good Day” using an if-else condition:
JavaScript
var time = new Date().getHours();
if (time < 12) {
console.log(“Good morning!”);
} else if (time < 18) {
console.log(“Good day!”);
} else {
console.log(“Good evening!”);
}
This code first gets the current time of day using the Date object’s getHours() method. The getHours() method returns the current hour as an integer between 0 and 23.
The code then uses an if-else condition to determine what greeting to print. If the current hour is less than 12, the code prints “Good morning!”. If the current hour is between 12 and 18, the code prints “Good day!”. Otherwise, the code prints “Good evening!”.
Here is an example of the output of this code:
Code snippet
11:00 AM
Good morning!
12:00 PM
Good day!
6:00 PM
Good evening!