- 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>
<!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>