Skip to content

Instantly share code, notes, and snippets.

@wrightwriter
Created February 16, 2019 12:30
Show Gist options
  • Save wrightwriter/f90d251cad37bba9eb506ad703db4399 to your computer and use it in GitHub Desktop.
Save wrightwriter/f90d251cad37bba9eb506ad703db4399 to your computer and use it in GitHub Desktop.
Function to generate a dynamic sorted calendar object in JavaScript.
function makeCalendar(startYear, endYear) {
let calendar = {};
var dateA = new Date(startYear, 0, 1, 0, 0, 0);
var dateB = new Date(endYear, 11, 36, 0, 0, 0);
for (
var myDate = dateA;
myDate <= dateB;
myDate = new Date(myDate.getTime() + 1000 * 60 * 60 * 24)
) {
let year = myDate.getFullYear();
let month = myDate.getMonth() + 1;
let dayOfMonth = myDate.getDate();
let dayOfWeek = myDate.getDay() + 1;
if ( calendar[year] == undefined ){
calendar[year] = {};
}
if ( calendar[year][month] == undefined ){
calendar[year][month] = {};
}
// console.log(year, month, dayOfMonth, dayOfWeek);
calendar[year][month][dayOfMonth] = {};
calendar[year][month][dayOfMonth].dayOfWeek = dayOfWeek;
}
return calendar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment