Skip to content

Instantly share code, notes, and snippets.

@arnabdas
Created January 6, 2021 12:57
Show Gist options
  • Save arnabdas/c3cf4502377a002d55f4dbf4bb7a61c8 to your computer and use it in GitHub Desktop.
Save arnabdas/c3cf4502377a002d55f4dbf4bb7a61c8 to your computer and use it in GitHub Desktop.
Simple calendar object creation utility
(function() {
'use strict';
var month = 1;
var year = 2021;
var sqlResponse = [
'2019-12-01',
'2019-12-03',
'2019-12-14',
'2019-12-17'
];
var inspectionAllocatedDays = {};
for (var k = 0; k < sqlResponse.length; k++) {
var dateStrSplit = sqlResponse[k].split('-');
var day = dateStrSplit[dateStrSplit.length - 1];
inspectionAllocatedDays[day] = true;
}
const d = (y, m) => new Date(y, m, 0).getDate();
var daysInMonth = d(year, month);
var firstBlankDays = new Date(year, month - 1, 1).getDay();
var numIterations = Math.floor((firstBlankDays + daysInMonth) / 7) + (firstBlankDays === 0 ? 0 : 1);
var monthDayCounter = 1;
var responseMonthObj = {};
for (var i = 1; i <= numIterations; i++) {
if (!responseMonthObj[i]) {
responseMonthObj[i] = [];
}
for (var j = 0; j < 7; j++) {
if (i === 1 && j < firstBlankDays) {
responseMonthObj[i].push({
day: 0
});
continue;
}
if (monthDayCounter <= daysInMonth) {
responseMonthObj[i].push({
day: monthDayCounter,
isAllocated: inspectionAllocatedDays[monthDayCounter++] ? true : false
});
}
}
}
var lastDayOfMonth = new Date(year, month, 0).getDay();
if (lastDayOfMonth !== 6) {
var lastWeekArr = responseMonthObj[numIterations];
for (var l = lastDayOfMonth; l < 6; l++) {
lastWeekArr.push({
day: 0
});
}
}
console.log(Object.values(responseMonthObj));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment