-9

I have found this code to display each image on the respective day. Please can someone explain the following code and How can I do it for 365 days a year instead of days in a week?

<script type="text/javascript"><!--
var imlocation = "images/";
 function ImageArray (n) {
   this.length = n;
   for (var i =1; i <= n; i++) {
     this[i] = ' '
   }
 }
image = new ImageArray(7);
image[0] = 'sunday.gif';
image[1] = 'monday.gif';
image[2] = 'tuesday.gif';
image[3] = 'wednesday.gif';
image[4] = 'thursday.gif';
image[5] = 'friday.gif';
image[6] = 'saturday.gif';
var currentdate = new Date();
var imagenumber = currentdate.getDay();
document.write('<img src="' + imlocation + image[imagenumber] + '">');
//--></script>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • I see you get downvoted. A hint to help you: Your script is probably 15 years old, if not more. Do not do it like that. Avoid document.write. This can be done much more elegant theese days. And one more thing: What are you asking for? This script places an image for monday, tuesday, etc. It is working every day in the year. – Erwin Moller Nov 30 '15 at 13:29

1 Answers1

-1

Your code generate an array and put images into each key. Please read documentation for used functions like: http://www.w3schools.com/jsref/jsref_getDay.asp

Fast change (not so tidy) into image for a day of a year:

 // http://stackoverflow.com/a/8619946
 var now = new Date();
 var start = new Date(now.getFullYear(), 0, 0);
 var diff = now - start;
 var oneDay = 1000 * 60 * 60 * 24;
 var day = Math.floor(diff / oneDay);

 var imlocation = "images/";

 function ImageArray(n) {
   this.length = n;
   for (var i = 1; i <= n; i++) {
     this[i] = ' '
   }
 }

 image = new ImageArray(365);
 image[0] = 'sunday.gif';
 // ...
 // image[365]...

 document.write('<img src="' + imlocation + image[day] + '">');
 //--></script>
Damian P
  • 1
  • 1