Converts between Anno Domini (AD/Gregorian) and Bikram Sambat (BS/Nepali) dates.
npm install bs-dateA JavaScript library for converting between Anno Domini (AD/Gregorian) and Bikram Sambat (BS/Nepali) dates.
``bash`
npm install bs-date
- Convert Bikram Sambat dates to Anno Domini dates
- Convert Anno Domini dates to Bikram Sambat dates
- Supports BS years 2000-2090 (AD 1943-2034)
- Validates date ranges and throws appropriate errors
- Extends native Date prototype with toBS() methodtoNepali()
- Format dates in Nepali script with
- Get month names in Nepali or romanized format
- Get day names in Nepali, romanized, or localized English format
- Convert numbers to Nepali numerals
`javascript
import BSDate from "bs-date";
import "bs-date/src/number-utils.js"; // For Nepali number conversion
// Create a BS date
const bsDate = new BSDate(2082, 5, 25);
// Convert to AD
const adDate = bsDate.toAD();
console.log(adDate); // 2025-09-10T00:00:00.000Z
// Convert AD date to BS
const adDate2 = new Date("2025-09-10");
const bsDate2 = BSDate.fromAD(adDate2);
console.log(bsDate2); // BSDate { year: 2082, month: 5, day: 25 }
// Format in Nepali
console.log(bsDate.toNepali()); // "भाद्र २५, २०८२"
// Get month and day names
console.log(bsDate.monthName()); // "भाद्र"
console.log(bsDate.monthName({ romanized: true })); // "Bhadra"
console.log(bsDate.dayName()); // "मंगलवार"
console.log(bsDate.dayName({ romanized: true })); // "Mangalbar"
console.log(bsDate.dayName({ localized: true })); // "Tuesday"
`
`javascript
import "bs-date/src/date-utils.js";
const adDate = new Date("2025-09-10");
const bsDate = adDate.toBS();
console.log(bsDate); // BSDate { year: 2082, month: 5, day: 25 }
`
For displaying dates in Nepali format using native JavaScript localization:
`javascript
const adDate = new Date("2025-09-10");
// Format AD date in Nepali locale (displays AD date in Nepali numerals)
console.log(adDate.toLocaleDateString("ne-NP")); // "२०२५/९/१०"
// For BS date formatting, combine both approaches
const bsDate = adDate.toBS();
console.log(BS Date: ${bsDate.toNepali()}); // "BS Date: भाद्र २५, २०८२"AD Date (Nepali): ${adDate.toLocaleDateString("ne-NP")}
console.log(); // "AD Date (Nepali): २०२५/९/१०"`
Note: toLocaleDateString('ne-NP') formats the AD date in Nepali numerals and locale format, while toBS() and toNepali() provides the actual BS calendar date in Nepali script.
#### Constructor
`javascript`
new BSDate(year, month, day);
- year (number): BS year (2000-2090)month
- (number): BS month (1-12)day
- (number): BS day (1-32, varies by month)
#### Methods
##### toAD()
Converts the BS date to an AD Date object.
Returns: Date - The equivalent AD date
Throws: BSDateOutOfRangeError - If the BS date is invalid or out of range
##### toNepali()
Formats the BS date in Nepali script.
Returns: string - The formatted date (e.g., "भाद्र २५, २०८२")
##### monthName(options)
Gets the month name in Nepali or romanized format.
Parameters:
- options (object, optional):romanized
- (boolean): If true, returns romanized month name
Returns: string - The month name
Examples:
`javascript`
bsDate.monthName(); // "भाद्र"
bsDate.monthName({ romanized: true }); // "Bhadra"
##### dayName(options)
Gets the day name in various formats.
Parameters:
- options (object, optional):romanized
- (boolean): If true, returns romanized day namelocalized
- (boolean): If true, returns English day name
Returns: string - The day name
Throws: Error - If both romanized and localized are true
Examples:
`javascript`
bsDate.dayName(); // "मंगलवार"
bsDate.dayName({ romanized: true }); // "Mangalbar"
bsDate.dayName({ localized: true }); // "Tuesday"
##### BSDate.fromAD(adDate)
Static method to create a BSDate from an AD Date.
Parameters:
- adDate (Date): The AD date to convert
Returns: BSDate - The equivalent BS date
#### Date.prototype.toBS()
Converts an AD Date to BSDate.
Returns: BSDate - The equivalent BS date
Throws: DateOutOfRangeError - If the AD date is before April 14, 1943
#### Number.prototype.toNepali()
Converts a number to Nepali numerals.
Returns: string - The number in Nepali numerals
Examples:
`javascript
import "bs-date/src/number-utils.js";
(123).toNepali(); // "१२३"
(2082).toNepali(); // "२०८२"
`
- BS Range: 2000/01/01 - 2090/12/30
- AD Range: April 14, 1943 - April 13, 2034
The library throws two types of errors:
Thrown when:
- BS year is outside the supported range (2000-2090)
- BS month is invalid (not 1-12)
- BS day is invalid for the given month/year
- Required constructor parameters are missing
Thrown when:
- AD date is before the supported epoch (April 14, 1943)
`javascript
import BSDate, { BSDateOutOfRangeError } from "bs-date";
try {
const invalidDate = new BSDate(1999, 1, 1);
invalidDate.toAD();
} catch (error) {
if (error instanceof BSDateOutOfRangeError) {
console.log("BS date is out of range");
}
}
`
`javascript
import BSDate from "bs-date";
import "bs-date/src/date-utils.js";
import "bs-date/src/number-utils.js";
const today = new Date();
const todayBS = today.toBS();
console.log(Today in BS: ${todayBS.year}/${todayBS.month}/${todayBS.day});Today in Nepali: ${todayBS.toNepali()}
console.log();Today is ${todayBS.dayName({ localized: true })}
console.log();`
`javascript
import BSDate from "bs-date";
import "bs-date/src/number-utils.js";
const bsDate = new BSDate(2082, 5, 18);
// Different formatting options
console.log("Default format:", bsDate.toNepali()); // "भाद्र १८, २०८२"
console.log("Month (Nepali):", bsDate.monthName()); // "भाद्र"
console.log("Month (English):", bsDate.monthName({ romanized: true })); // "Bhadra"
console.log("Day (Nepali):", bsDate.dayName()); // "बुधबार"
console.log("Day (English romanized):", bsDate.dayName({ romanized: true })); // "Budhbar"
console.log("Day (Localized):", bsDate.dayName({ localized: true })); // "Wednesday"
// Number conversion
console.log("Year in Nepali:", bsDate.year.toNepali()); // "२०८२"
console.log("Day in Nepali:", bsDate.day.toNepali()); // "१८"
`
The library includes comprehensive data for Bikram Sambat calendar:
- Months: Baisakh, Jeth, Asar, Shrawan, Bhadra, Ashwin, Kartik, Mangsir, Poush, Magh, Falgun, Chaitra
- Variable month lengths depending on the year
- Accurate conversion based on established BS calendar data
`bash`
npm test
`bash`
npm run lint
`bash``
npm run dev
This project is licensed under the MIT License - see the LICENSE file for details.
Deepak Lamichhane