Date Converter
ADtoBS()
Pure helper function to convert Gregorian calendar date strings into Bikram Sambat date strings.
The ADtoBS function performs direct mathematical conversion of a Gregorian date string into its corresponding Bikram Sambat date string. It accepts standard date formats and returns the converted Nepali date, handling month day variations and leap year calculations automatically without external dependencies.
Import
import { ADtoBS } from "nepali-date-library";const { ADtoBS } = require("nepali-date-library");Signature
function ADtoBS(adDate: string): string;Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
adDate | string | Yes | Gregorian date string formatted strictly as YYYY-MM-DD. |
Return Value
- Type:
string - Format:
YYYY-MM-DDrepresenting the corresponding Bikram Sambat date.
Exceptions
| Error Message | Trigger Condition |
|---|---|
Invalid date format. Expected format: YYYY-MM-DD | Thrown if adDate fails the format regex ^\d{4}-\d{2}-\d{2}$. |
Invalid date input '<adDate>' | Thrown if date values produce NaN (e.g. 2024-02-31). |
DateConversionError: Failed to convert AD to BS | Thrown if the date falls outside the verified ephemeris boundary (1919-04-13 to 2044-04-12 AD). |
Examples
Basic Conversion
import { ADtoBS } from "nepali-date-library";
const bsDate = ADtoBS("2024-10-31");
console.log(bsDate); // "2081-07-15"Batch Array Processing
import { ADtoBS } from "nepali-date-library";
const records = [
{ id: 1, createdAt: "2024-01-01" },
{ id: 2, createdAt: "2024-04-13" },
{ id: 3, createdAt: "2024-10-31" },
];
const mappedRecords = records.map((record) => ({
...record,
createdAtBS: ADtoBS(record.createdAt),
}));
console.log(mappedRecords[0].createdAtBS); // "2080-09-16"Error Handling
import { ADtoBS } from "nepali-date-library";
function safeADtoBS(dateString: string): string | null {
try {
return ADtoBS(dateString);
} catch {
return null;
}
}
console.log(safeADtoBS("2024-10-31")); // "2081-07-15"
console.log(safeADtoBS("invalid")); // nullRelated
BStoAD(): Reciprocal conversion from BS to AD.new NepaliDate(date): Converting a JavaScript Date object into a fullNepaliDateinstance.
Advertisement
Date Converter Guide
Comprehensive guide to converting dates between Gregorian and Bikram Sambat calendar systems using standalone helper functions.
BStoAD()
Pure helper function to convert Bikram Sambat date strings into Gregorian calendar date strings.