let month = moment().month();
let lastDays = moment(month).daysInMonth();
let startOf = moment().startOf("month");
let startWeek = startOf.day();
let weeks = [];
let week = {};
let currentDay = 1;
let currentWeek = 1;
let weeksday = 1;
let lastDate = lastDays + 1;
if(startWeek !== 1) {
week = {};
for(let i = 1; i <= 7; i++) {
if(startWeek <= i) {
week[i] = currentDay;
currentDay++;
} else {
week[i] = "";
}
}
weeks[currentWeek] = week;
currentWeek ++;
week = {};
}
while(currentDay !== lastDate) {
week[weeksday] = currentDay;
if(weeksday === 7) {
weeks[currentWeek] = week;
week = {};
weeksday = 1;
} else if(currentDay === lastDays) {
weeks[currentWeek] = week;
} else {
weeksday++;
}
currentDay++;
}
const getFirstDateofMonth = date => new Date(date.getFullYear(), date.getMonth(), 1).getDay();
const getDaysInMonth = date => new Date(date.getFullYear(), date.getMonth()+1, 0).getDate();
const createMonth = (date, el) => {
const startFrom = getFirstDateofMonth(date); // Return the number of the first day
const daysAmount = getDaysInMonth(date); // Return the number of days in the month
for (let i = 1; i < startFrom+daysAmount; i++) {
const li = document.createElement('li');
// Colorize cells
if (i < startFrom) {
li.classList.add('past');
} else {
li.classList.add('current')
}
el.appendChild(li);
}
}
const calendar = document.getElementById('calendar'); // Container
const date = new Date(Date.now()); // Date
/*
date - some date in format:
- new Date('November 17, 1995 03:24:00')
- new Date(Date.now())
- new Date('1995-12-17T03:24:00')
- ....
calendar - DOM element
*/
createMonth(date, calendar);