根据一天中的时间更改图像

Changes image based on time of day

大家晚上好。我是一名初级程序员,我需要你的帮助来根据一天中的时间更改图像。例如:

谁能帮帮我?我知道我需要使用 JS,但我不知道如何正确地实现它。谢谢

像这样的东西会起作用:

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'
}

将 myImage 更改为您的图像 ID,并将图像源更改为图像文件的链接。