Skip to content

Instantly share code, notes, and snippets.

@sosnovskyas
Last active December 22, 2016 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sosnovskyas/61e0a036b6c23f768cb59e4f1903b602 to your computer and use it in GitHub Desktop.
Save sosnovskyas/61e0a036b6c23f768cb59e4f1903b602 to your computer and use it in GitHub Desktop.
generate month (генерация месяца)
const generateMonth = (date) => {
const getLocalDay = (date) => {
// конвертация в европейскую неделю
let day = date.getDay();
if (day == 0) { // день 0 становится 7
day = 7;
}
return day;
};
const currentMonth = date.getMonth();
const firstDate = new Date(date.getFullYear(), currentMonth, 1);
const lastDate = new Date(date.getFullYear(), currentMonth + 1, 0);
let month = [];
let week = [];
// заполнение пустых дней первой недели
for (let i = 1; i < getLocalDay(firstDate); i++) {
week.push('');
}
for (let i = 1; i <= lastDate.getDate(); i++) {
const tmpDate = new Date(date.getFullYear(), currentMonth, i);
if (getLocalDay(tmpDate) == 1) {
if (week.length) month.push(week);
week = [];
}
week.push(i);
}
// заполнение пустых дней последней недели
for (let i = getLocalDay(lastDate); i < 7; i++) {
week.push('');
}
if (week.length) month.push(week);
return month;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment