New to coding - image to the time of day based on time

Joined
May 14, 2022
Messages
1
Reaction score
0
Hi all, trying to write a code in JS and HTML to display an image based on the day of time. trying to use 4 images, morning, noon, sunset then night. this is what I have so far, but it's not running properly for me.

<!DOCTYPE html>
<html>
<head>
</head>

<<body>
<img src="" alt="" id="image_1" />
</body>

<script>
var day = new Date();
var today =parseInt(day.getDay());
var hour = parseInt(day.getHours());
var minutes = parseInt(day.getMinutes());
var imgElem=document.getElementById('image_1');

(function(){
if (day === 1) {
if (hour >= 6 && min >= 01 && hour < 12) {
img = "image1"; // for when between 6:00 and 12
} else if (hour >= 12 && hour < 1) {
img = "image2"; // for between 12 and 1
} else if (hour >= 1 && min >= 01 && hour < 18) {
img = "image3"; // for between 6 and 9
} else {
img = "image4"; // for between 18 and 6
}
}

})();
</script>
</html>
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
Please use the "code" capsule. It's har to read.
What exactly isn't going well? imgElem never ever will have an image, because you missed something.

Code:
imgElem.src=img;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top