Nepali DateNepali DateBikram Sambat Date Engine
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

ParameterTypeRequiredDescription
adDatestringYesGregorian date string formatted strictly as YYYY-MM-DD.

Return Value

  • Type: string
  • Format: YYYY-MM-DD representing the corresponding Bikram Sambat date.

Exceptions

Error MessageTrigger Condition
Invalid date format. Expected format: YYYY-MM-DDThrown 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 BSThrown 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")); // null

Advertisement

On this page