Change the image according to the day

I want to add functionality to my site, which changes the displayed images depending on the day. I could not completely wrap myself around as I am a little newbie. My images are stored in / week 2 / image_day_4 images. I will try to use a pair of incremental variables to access the image values, so if day is day 4 in week 2 of membership, the code should read something like

"images/week_" + week + "/image_day_" + day;

every day after the start of membership, I increase the day.

Is there a better way to do this?

+4
source share
2 answers

, , . ? JS . , URL- . :

JS:

var obj = {
    key1: value1,
    key2: value2
};

JS:

var membershipImages = {
    1: "images/day1.jpg",
    2: "images/thatThing.png"
};

, , , , URL-:

someObject.setSomeProperty(membershipImages.getProperty("1"));
+2

, :

function getWeekNumber(d) {
    // Copy date so don't modify original
    d = new Date(+d);
    d.setHours(0, 0, 0);
    // Set to nearest Thursday: current date + 4 - current day number
    // Make Sunday day number 7
    d.setDate(d.getDate() + 4 - (d.getDay() || 7));
    // Get first day of year
    var yearStart = new Date(d.getFullYear(), 0, 1);
    // Calculate full weeks to nearest Thursday
    var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7)
    // Return array of year and week number
    return weekNo;
}

var path = "images/week_" + getWeekNumber(new Date()) + "/image_day_" + new Date().getDay();
alert(path);

JSFiddle

EDIT: new Date() , .

+1

All Articles