Language

Calendar-grid helpers

Building blocks for rendering a month view, like a typical calendar UI.

function firstWeekdayOfBsMonth(year: number, month: number): Weekday

The day of the week (0 is Sunday) that day 1 of the given month falls on.

function weeksInBsMonth(year: number, month: number): number

The number of rows (weeks, Sunday to Saturday) a grid of the month needs — always the length of getBsMonthCalendar's result.

function getBsMonthCalendar(year: number, month: number): (BSDate | null)[][]

A week-by-week grid of the month. Each week is exactly 7 cells, Sunday first, as Nepali calendars are usually laid out. A null cell means no day of this month falls there — padding before day 1 and after the last day. Every day of the month appears exactly once, in order. Matches go-bs's MonthCalendar, and all three throw InvalidBSDateError for an unsupported year or month.

import { firstWeekdayOfBsMonth, getBsMonthCalendar, weeksInBsMonth } from "bikram-sambat-ts";
 
firstWeekdayOfBsMonth(2083, 6); // 4 (Thursday)
weeksInBsMonth(2083, 6); // 5
 
for (const week of getBsMonthCalendar(2083, 6)) {
  console.log(week.map((cell) => (cell ? String(cell.day).padStart(2) : "  ")).join(" "));
}
 
// Su Mo Tu We Th Fr Sa
//              1  2  3
//  4  5  6  7  8  9 10
// 11 12 13 14 15 16 17
// 18 19 20 21 22 23 24
// 25 26 27 28 29 30 31

In React, each week maps straight to a table row:

<tbody>
  {getBsMonthCalendar(2083, 6).map((week, i) => (
    <tr key={i}>
      {week.map((cell, j) => (
        <td key={j}>{cell?.day}</td>
      ))}
    </tr>
  ))}
</tbody>

Try it

This grid is rendered live, in your browser, by the published bikram-sambat-ts package's getBsMonthCalendar — not a reimplementation. Today's date in Nepal is highlighted.

Ashwin 2083(असोज)
SuMoTuWeThFrSa
123
45678910
11121314151617
18192021222324
25262728293031