Skip to content

Instantly share code, notes, and snippets.

@ear1grey
Created August 3, 2012 16:26
Show Gist options
  • Save ear1grey/3249202 to your computer and use it in GitHub Desktop.
Save ear1grey/3249202 to your computer and use it in GitHub Desktop.
A tiny utility for creating a json array of dates 1 week apart
/*
A tiny utility for creating a json array of dates.
I need this for creating year-long timetables where after an initial start
we have a few weeks off for christmas, then easter, and these weeks differ
each year, so the 'switches' make it easy to include weeks (signified by a 1)
or exclude weeks (a 0)
Note that monts start at 0 not 1 :-)
*/
var d, switches, num, weeks, week;
weeks = [];
d = new Date(2012, 8, 24);
switches = [1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,1];
num = switches.reduce(function(p, c){
return p + c;
});
for (week = 0; week < 99; week++) {
// if the week is a "1" in the array of
if (switches[week] == 1) {
weeks.push(
d.toString()
);
}
// add seven days
d.setDate(d.getDate()+7);
}
console.log(weeks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment