Calendar Utilities
calendar-utils provides a set of utility functions designed to generate calendar views, abstracting away the complex date calculations required for displaying events across different time spans. It is currently at version 0.12.5, indicating a pre-1.0 release stage where API stability might be less guaranteed and breaking changes between minor versions are possible. The library is date library-agnostic, supporting popular options like `date-fns`, `luxon`, and `moment` through peer dependencies, allowing developers to choose their preferred date manipulation library. Its primary differentiator is its focus purely on view generation logic, making it a flexible backend for any calendar UI component, rather than a full-fledged UI solution itself. It ships with full TypeScript types, enhancing developer experience and type safety.
Common errors
-
Error: Cannot find module 'date-fns' (or 'luxon' or 'moment')
cause A required peer dependency for date manipulation (e.g., date-fns) is not installed in your project.fixInstall the missing date library: `npm install date-fns` or `npm install luxon` or `npm install moment`. -
TypeError: getCalendarMonthView is not a function
cause This usually means you are attempting to use a CommonJS `require` statement on a library primarily designed for ESM, or using incorrect named/default import syntax.fixEnsure you are using ESM `import { getCalendarMonthView } from 'calendar-utils';` and your project supports ESM. -
Argument of type 'string' is not assignable to parameter of type 'Date'.
cause The `calendar-utils` functions expect native JavaScript `Date` objects (or objects compatible with the chosen date library's `Date` type) for date parameters, not strings.fixEnsure all date inputs (e.g., `date`, `viewStart`, `viewEnd` arguments) are `new Date()` objects, or parsed by your chosen date library (e.g., `parseISO('2023-01-01')` from `date-fns`).
Warnings
- breaking As a library in `0.x.x` versioning, `calendar-utils` may introduce breaking changes between minor versions (e.g., `0.11.0` to `0.12.0`) without adhering to strict semantic versioning. Always review the changelog when upgrading.
- gotcha The library relies on peer dependencies for date manipulation (e.g., `date-fns`, `luxon`, `moment`). You must install one of these explicitly in your project, along with any necessary adapters if the library requires it (though for `calendar-utils` the functions directly accept standard Date objects or compatible date library types).
- gotcha Incorrect configuration of `weekStartsOn` or `weekendDays` can lead to unexpected calendar layouts, especially when mixing `calendar-utils` with UI components that have their own default settings.
Install
-
npm install calendar-utils -
yarn add calendar-utils -
pnpm add calendar-utils
Imports
- getCalendarMonthView
const { getCalendarMonthView } = require('calendar-utils');import { getCalendarMonthView } from 'calendar-utils'; - getCalendarWeekView
import getCalendarWeekView from 'calendar-utils';
import { getCalendarWeekView } from 'calendar-utils'; - getCalendarDayView
import * as calendarUtils from 'calendar-utils';
import { getCalendarDayView, getWeekViewHeader } from 'calendar-utils';
Quickstart
import { getCalendarMonthView } from 'calendar-utils';
import { startOfMonth, endOfMonth, eachDayOfInterval, format, addMonths } from 'date-fns';
interface CalendarDay {
date: Date;
isToday: boolean;
isWeekend: boolean;
events: string[];
}
const currentDate = new Date();
const monthStart = startOfMonth(currentDate);
const monthEnd = endOfMonth(currentDate);
const calendarMonthView = getCalendarMonthView({
date: currentDate,
events: [], // Your events array, e.g., [{ start: new Date(), end: new Date(), title: 'Meeting' }]
weekStartsOn: 0, // Sunday
excluded: [], // e.g., [6] for Saturday
viewStart: monthStart,
viewEnd: monthEnd,
hourSegments: 2, // Not directly used by month view, but part of config
dayStartHour: 0,
dayEndHour: 23,
weekendDays: [0, 6]
});
console.log(`Month View for ${format(currentDate, 'MMMM yyyy')}:`);
calendarMonthView.days.forEach(day => {
console.log(` ${format(day.date, 'yyyy-MM-dd')} (isCurrentMonth: ${day.inMonth}): ${day.events.length} events`);
});
// Example of getting a day outside the current month (for navigation)
const nextMonth = addMonths(currentDate, 1);
const nextMonthView = getCalendarMonthView({
date: nextMonth,
events: [],
weekStartsOn: 0,
excluded: [],
viewStart: startOfMonth(nextMonth),
viewEnd: endOfMonth(nextMonth),
hourSegments: 2,
dayStartHour: 0,
dayEndHour: 23,
weekendDays: [0, 6]
});