mnth: Calendar Month Generator

2.0.0 · active · verified Tue Apr 21

mnth is a lightweight, framework-agnostic utility library designed to simplify the generation of calendar-style month data. Its primary function, `getCalendarMonth`, takes a `Date` object and an optional configuration (e.g., `firstDayOfWeek`) to return a 2D array of `Date` objects, representing all days visible in a given month's calendar view, including leading and trailing days from adjacent months. Currently at version 2.0.0, the package provides a focused solution for UI components like datepickers and full calendars, distinguishing itself by providing raw `Date` objects for maximum flexibility rather than pre-formatted strings or complex component structures. It ships with TypeScript types, promoting strong type checking and improved developer experience. The library is part of the `nextools/metarepo` monorepo.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `getCalendarMonth` to generate a 2D array of `Date` objects for a given month, showing both default and custom `firstDayOfWeek` options. It then formats and logs the day numbers for each week.

import { getCalendarMonth } from 'mnth';

// Get the calendar month for April 2018, with default options (Monday as first day of week)
const date = new Date('2018-04-01T12:00:00.000Z'); // Using UTC to avoid local timezone issues on initialization
const calendarMonth = getCalendarMonth(date);

console.log('Calendar for April 2018 (default options):');
calendarMonth.forEach(week => {
  console.log(week.map(day => day.toLocaleDateString('en-US', { day: '2-digit' })).join(', '));
});

// Example with custom options: Sunday as the first day of the week
const sundayFirstCalendar = getCalendarMonth(date, { firstDayOfWeek: 0 });

console.log('\nCalendar for April 2018 (Sunday as first day):');
sundayFirstCalendar.forEach(week => {
  console.log(week.map(day => day.toLocaleDateString('en-US', { day: '2-digit' })).join(', '));
});

/* Expected output structure (values depend on exact date/timezone):
Calendar for April 2018 (default options):
26, 27, 28, 29, 30, 31, 01
02, 03, 04, 05, 06, 07, 08
09, 10, 11, 12, 13, 14, 15
16, 17, 18, 19, 20, 21, 22
23, 24, 25, 26, 27, 28, 29
30, 01, 02, 03, 04, 05, 06

Calendar for April 2018 (Sunday as first day):
25, 26, 27, 28, 29, 30, 31
01, 02, 03, 04, 05, 06, 07
08, 09, 10, 11, 12, 13, 14
15, 16, 17, 18, 19, 20, 21
22, 23, 24, 25, 26, 27, 28
29, 30, 01, 02, 03, 04, 05
*/

view raw JSON →