Supporting the Nepali (BS) Calendar in Business Software

Bikram Sambat is not a display format. How it changes date storage, period boundaries, fiscal years, reports, and the cases that only bite at year end.

3 min read

Every software project in Nepal reaches the same fork, usually in month three, usually described as a small feature: “can the dates be in Bikram Sambat?” It is not a display format. It changes what a month is, what a fiscal year is, and therefore what every report groups by.

Treating it as formatting is the mistake, and it is expensive to undo.

Store one calendar, present two

The rule that survives: store Gregorian, convert at the edges. Store timestamptz in UTC as you would anywhere. Convert to BS for display, for report grouping, and for period boundaries. Never store a BS string as the canonical date.

The one exception is a BS period key for reporting. A materialised bs_period column, 2082-09, alongside the real timestamp is legitimate, because it turns month grouping into a plain equality filter and it is derivable from the timestamp, so it cannot drift.

Conversion, and where it gets hard

The BS calendar is not algorithmic. It is a lookup: each year has a published table of month lengths, and those tables have changed historically. So a converter is a data file plus arithmetic, not a formula.

// A month-length table is the whole calendar, and it is not derivable.
// Each entry is the twelve month lengths for one BS year.
const BS_MONTH_DAYS: Record<number, readonly number[]> = {
  2081: [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
  2082: [31, 31, 32, 31, 31, 30, 30, 29, 30, 29, 30, 30],
};

/** Days since the BS epoch, by summing whole years then whole months. */
function bsToDayIndex(year: number, month: number, day: number): number {
  let days = 0;
  for (let y = 2000; y < year; y += 1) days += BS_MONTH_DAYS[y].reduce((a, b) => a + b, 0);
  for (let m = 1; m < month; m += 1) days += BS_MONTH_DAYS[year][m - 1];
  return days + day - 1;
}

Three consequences follow from that being a table:

  • The table has to be extended, not calculated. A new BS year means new data, published annually. Ship the table with the app and make updating it a data task, not a code change.
  • Never use new Date('2082-09-15'). JavaScript will silently parse that as an invalid or wildly different Gregorian date. Conversions go through your own function or not at all.
  • Month lengths differ. A BS month is 29 to 32 days. Any code that assumes 30 is wrong for most of the year and right often enough to pass testing.

Fiscal year is not the calendar year

The Nepali fiscal year runs roughly mid-July to mid-July in Gregorian terms, Shrawan to Ashadh in BS. Two traps live here:

  1. 01

    The boundary is a date, not a month

    The fiscal year starts on Shrawan 1, which falls on a different Gregorian date each year. Configuring it as "July" produces one wrong report every year.

  2. 02

    Reports group by fiscal period, not calendar month

    A "this year" report means the fiscal year, and a dashboard that groups by Gregorian January-December will not reconcile to a filing. This is the requirement that made it an issue in a restaurant ERP.

  3. 03

    Two systems, one number

    If the accounting package reports on the fiscal year and your product reports on the calendar year, the two will disagree every January, and the product will be blamed.

Reports and period boundaries

The practical approach is a single function that answers “what is the start and end instant of this BS month or fiscal year”, and for every report to call it rather than constructing dates inline.

/** Half-open interval [start, end) in UTC for a BS month. */
function bsMonthRange(year: number, month: number): { start: Date; end: Date } {
  const start = bsEpoch.add(bsToDayIndex(year, month, 1), 'days');
  const end = bsEpoch.add(bsToDayIndex(year, month, monthLength(year, month)) + 1, 'days');
  return { start: start.toDate(), end: end.toDate() };
}

Half-open intervals remove the whole class of “is the last day included” questions, and they compose: a fiscal year is just twelve of these stacked.

Edge cases worth an explicit test

  • The leap-ish month. BS month lengths change year to year, so the boundary test cannot use a fixed date. Test the first and last day of a month whose length changed from the previous year.
  • The fiscal new year. Assert that a transaction on the last day of Ashadh belongs to the old year and one on the first of Shrawan belongs to the new.
  • Year 32 in a month. A month with 32 days exists, and a naive <= 31 check does not.
  • A user entering a BS date that does not exist. Every converter needs a defined answer for the 31st of a 30-day month.

The reason to write these down is that they all pass by accident most of the time, which is the worst property a bug can have. The document that gets you here is the same one that should have listed this as a module rather than a detail, the PRD, and the same discipline as any good requirements pass.

On this page

Writing a PRD for a Restaurant ERP Before Code

Four modules, the user stories that shaped them, the scope cuts that saved the project, and why the calendar is a requirement, not polish.

3 min read

Building a KYC Dashboard Across a Branch Network

Seven iterations of a KYC reporting dashboard, what each one added, and why the first version should have been whatever branch managers would act on.

3 min read

Why a Standalone HTML Dashboard Beat a Heavy BI Tool

A single self-contained HTML file, refreshed by a scheduled script, beat a licensed BI platform. Where that approach wins, and where it stops scaling.

3 min read