Language

Formatting & Nepali

Custom layout strings in English or Nepali, month and weekday names, and converting between ASCII and Devanagari digits.

function formatBsDate(date: BSDate, layout?: string, options?: { nepali?: boolean }): string
function formatBsDate(date: BSDate, options?: { nepali?: boolean }): string

Renders date according to layout (default "YYYY-MM-DD", the form parseBsDate accepts), replacing the tokens below — the same tokens and longest-match-first rule as go-bs's Format. With { nepali: true }, numbers are rendered in Devanagari digits and names in Nepali, matching go-bs's FormatNepali. Any other character is copied through as-is, including ASCII digits in Nepali mode. Throws InvalidBSDateError if date is invalid.

TokenMeaningEnglishnepali: true
YYYY4-digit year2083२०८३
YY2-digit year83८३
MMMMmonth nameAshwinअसोज
MMmonth, zero-padded06०६
Mmonth6
DDday, zero-padded06०६
Dday6
ddddweekday nameTuesdayमंगलवार
dddshort weekday nameTueमंगल
import { formatBsDate } from "bikram-sambat-ts";
 
const d = { year: 2083, month: 6, day: 6 };
 
formatBsDate(d); // "2083-06-06"
formatBsDate(d, "dddd, MMMM D, YYYY"); // "Tuesday, Ashwin 6, 2083"
formatBsDate(d, "D/M/YY"); // "6/6/83"
 
formatBsDate(d, { nepali: true }); // "२०८३-०६-०६"
formatBsDate(d, "dddd, MMMM D, YYYY", { nepali: true }); // "मंगलवार, असोज ६, २०८३"
formatBsDate(d, "ddd D/M/YY", { nepali: true }); // "मंगल ६/६/८३"
formatBsDate(d, "मिति: YYYY-MM-DD", { nepali: true }); // "मिति: २०८३-०६-०६"

There's no escaping and no MMM token: Nepali month names don't abbreviate unambiguously (Ashadh and Ashwin both start "Ash"). A literal D or M in a layout is always a token, so MMM decomposes into MM + M ("066" for Ashwin 6) rather than erroring — the same as go-bs.

Month and weekday names

function getBsMonthName(month: number): string
function getBsMonthNameNepali(month: number): string

The English or Devanagari name of a 1-based BS month, e.g. "Ashwin" / "असोज" for 6. Throws InvalidBSDateError for a month outside 1–12.

function getBsWeekdayName(weekday: number): string
function getBsWeekdayNameNepali(weekday: number): string

The English or Devanagari name of a weekday numbered like Date#getDay() (0 is Sunday), e.g. "Sunday" / "आइतवार". Throws RangeError for a weekday outside 0–6.

import { getBsDayOfWeek, getBsMonthName, getBsMonthNameNepali, getBsWeekdayName, getBsWeekdayNameNepali } from "bikram-sambat-ts";
 
getBsMonthName(6); // "Ashwin"
getBsMonthNameNepali(6); // "असोज"
getBsWeekdayName(0); // "Sunday"
getBsWeekdayNameNepali(0); // "आइतवार"
getBsWeekdayNameNepali(getBsDayOfWeek({ year: 2083, month: 6, day: 6 })); // "मंगलवार"

The Nepali names follow Hamro Patro's calendar, the same source as the month names and the same spelling as go-bs: आइतवार, सोमवार, मंगलवार, बुधवार, बिहिवार, शुक्रवार, शनिवार. The Nepal Academy standard spelling uses -बार instead (आइतबार, …); both are in common use.

Nepali digits

function toNepaliDigits(value: string | number): string

Replaces every ASCII digit (0–9) with its Devanagari equivalent. Other characters pass through unchanged. A number is converted with String(value) first.

function fromNepaliDigits(value: string): string

The inverse: replaces every Devanagari digit (०–९) with its ASCII equivalent.

import { fromNepaliDigits, parseBsDate, toNepaliDigits } from "bikram-sambat-ts";
 
toNepaliDigits("2083-06-06"); // "२०८३-०६-०६"
toNepaliDigits(2083); // "२०८३"
fromNepaliDigits("२०८३-०६-०६"); // "2083-06-06"
 
parseBsDate(fromNepaliDigits("२०८३-०६-०६")); // { year: 2083, month: 6, day: 6 }