good evening all. I'am a beginner programmer, I need your help in changing the image based on the time of day. for example:
- 00-11am = image A
- 12pm-18pm = image B
- 19pm-23pm = image C
Can anyone help me? I know that I need to use JS but I don't know how to implement it properly. Thanks
CodePudding user response:
Something like this would work:
let currentTime = new Date().getHours();
let img = document.getElementById('myImage')
if (currentTime >= 0 && currentTime <= 11) {
img.src = 'image 1 link'
} else if (currentTime >= 12 && currentTime <= 18) {
img.src = 'image 2 link'
} else if (currentTime >= 19 && currentTime <= 23) {
img.src = 'image 3 link'
}
change myImage to whatever your images ID is, and change the image sources to the links to your image files.