如何将这一天的图像代码转换为白天更改图像,但是在固定时间而不是午夜

时间:2017-07-13 23:53:26

标签: javascript html image time

所以这是我有的代码

var imlocation = "../assets/oldham/";
function ImageArray (n) {
this.length = n;
for (var i =1; i <= n; i++) {
 this[i] = ' '
  }
 }
image = new ImageArray(7);
image[0] = 'sunday.jpg';
image[1] = 'monday.jpg';
image[2] = 'tuesday.jpg';
image[3] = 'wednesday.jpg';
image[4] = 'thursday.jpg';`enter code here`
image[5] = 'friday.png';
image[6] = 'saturday.png';
var currentdate = new Date();
var imagenumber = currentdate.getDay();
document.write('<img src="' + imlocation + image[imagenumber] + '">');`

目前它将改变午夜12点钟的照片但是因为它是一个深夜酒吧,我希望它在早上6点改变,我该怎么做?

1 个答案:

答案 0 :(得分:0)

下面的代码会考虑当天的小时,如果是在早上6点之前,代码会显示前一天的图像。

var imlocation = "../assets/oldham/";
function ImageArray (n) {
this.length = n;
for (var i =1; i <= n; i++) {
 this[i] = ' '
  }
 }
image = new ImageArray(7);
image[0] = 'sunday.jpg';
image[1] = 'monday.jpg';
image[2] = 'tuesday.jpg';
image[3] = 'wednesday.jpg';
image[4] = 'thursday.jpg';`enter code here`
image[5] = 'friday.png';
image[6] = 'saturday.png';
var currentdate = new Date();
var imagenumber = currentdate.getDay();
var hour = currentdate.getHours();

if (hour < 6) {
  imagenumber = imagenumber - 1;

  if (imagenumber == -1) imagenumber = 6;
}

document.write('<img src="' + imlocation + image[imagenumber] + '">');`