Created
February 16, 2019 12:25
-
-
Save wrightwriter/530b9e40347b004ef155a173a379d052 to your computer and use it in GitHub Desktop.
Function to generate a dynamic sorted calendar object in JavaScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeCalendar(startYear, endYear) { | |
let calendar = {}; | |
var dateA = new Date(startYear, 6, 1, 0, 0, 0); | |
var dateB = new Date(endYear, 6, 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