Question

Is there an algorithm [or a javascript code] that gets all the dates for a specific day in the year if we gave it a starting date ?

For Example:

Input: Tuesday , Dec, 11 2012.

Output: Dec, 18 2012, Dec, 25 2012, Jan, 1, 2013 .... etc.

Was it helpful?

Solution

How hard can it be?

var d1 = new Date('Tuesday , Dec, 11 2012');

for (var i = 0; i < 365; i += 7) {
    d1.setDate(d1.getDate() + 7)
    console.log(d1);  //outputs every tuesday for the next year
}

FIDDLE

OTHER TIPS

Very similar to adeneo's answer. Beat me to it, but I'll still share it. =p

var getDaysInYear = function(day, year) {
    var startDate = new Date(day + " " + year);
    var date = startDate;
    var dates = [];

    while (date.getFullYear() == year) {
        dates.push(date.toDateString()); // change this to change the output
        date.setTime(date.getTime() + 1000 * 7 * 24 * 60 * 60);
    }

    return dates;
};

// Returns an array of whatever is in dates.push().
var dates = getDaysInYear("Tuesday", 2013);
console.log(dates);

FIDDLE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top