Calendar Utilities

0.12.5 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a calendar month view using `getCalendarMonthView` with `date-fns` for date manipulation.

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]
});

view raw JSON →