lion/packages/calendar/src/utils/createMonth.js
Mikhail Bashkirov 9fc5488175 feat(calendar): add reusable calendar
Co-authored-by: Erik Kroes <erik.kroes@ing.com>
Co-authored-by: Gerjan van Geest <gerjan.van.geest@ing.com>
Co-authored-by: Thijs Louisse <thijs.louisse@ing.com>
Co-authored-by: Thomas Allmer <thomas.allmer@ing.com>
2019-05-13 17:46:00 +02:00

25 lines
863 B
JavaScript

import { createWeek } from './createWeek.js';
export function createMonth(date, { firstDayOfWeek = 0 } = {}) {
if (Object.prototype.toString.call(date) !== '[object Date]') {
throw new Error('invalid date provided');
}
const firstDayOfMonth = new Date(date);
firstDayOfMonth.setDate(1);
const monthNumber = firstDayOfMonth.getMonth();
const weekOptions = { firstDayOfWeek };
const month = {
weeks: [],
};
let nextWeek = createWeek(firstDayOfMonth, weekOptions);
do {
month.weeks.push(nextWeek);
const firstDayOfNextWeek = new Date(nextWeek.days[6].date); // last day of current week
firstDayOfNextWeek.setDate(firstDayOfNextWeek.getDate() + 1); // make it first day of next week
nextWeek = createWeek(firstDayOfNextWeek, weekOptions);
} while (nextWeek.days[0].date.getMonth() === monthNumber);
return month;
}