UNPKG

zvelte-datepicker

Version:

A datepicker fork from the version 2.0.4 of https://github.com/6eDesign/svelte-calendar with time selection fonctionality

61 lines (57 loc) 2.32 kB
const getCalendarPage = (month, year, dayProps, weekStart = 0) => { let date = new Date(year, month, 1); if(date.getDay() === 0 && weekStart === 1) { //fix a weird bug where the 1st od the month is not visible date.setDate(date.getDate() - 6); } else { date.setDate(date.getDate() - date.getDay() + weekStart); } let nextMonth = month === 11 ? 0 : month + 1; let weeks = []; while (date.getMonth() !== nextMonth || date.getDay() !== weekStart || weeks.length !== 6) { if (date.getDay() === weekStart) weeks.unshift({ days: [], id: `${year}${month}${year}${weeks.length}` }); const updated = Object.assign({ partOfMonth: date.getMonth() === month, day: date.getDate(), month: date.getMonth(), year: date.getFullYear(), date: new Date(date) }, dayProps(date)); weeks[0].days.push(updated); date.setDate(date.getDate() + 1); } weeks.reverse(); return { month, year, weeks }; }; const getDayPropsHandler = (start, end, selectableCallback) => { let today = new Date(); today.setHours(0, 0, 0, 0); return date => { const isInRange = date >= start && date <= end; return { isInRange, selectable: isInRange && (!selectableCallback || selectableCallback(date)), isToday: date.getTime() === today.getTime() }; }; }; export function getMonths(start, end, selectableCallback = null, weekStart = 0) { if(isValidDate(start)) start.setHours(0, 0, 0, 0); else start = new Date(); if(isValidDate(end)) end.setHours(0, 0, 0, 0); else end = new Date(); let endDate = new Date(end.getFullYear(), end.getMonth() + 1, 1); let months = []; let date = new Date(start.getFullYear(), start.getMonth(), 1); let dayPropsHandler = getDayPropsHandler(start, end, selectableCallback); while (date < endDate) { months.push(getCalendarPage(date.getMonth(), date.getFullYear(), dayPropsHandler, weekStart)); date.setMonth(date.getMonth() + 1); } return months; } export const areDatesEquivalent = (a, b) => a.getDate() === b.getDate() && a.getMonth() === b.getMonth() && a.getFullYear() === b.getFullYear(); export function isValidDate(date) { return date && Object.prototype.toString.call(date) === "[object Date]" && !isNaN(date); }