A JavaScript package to convert dates between Bikram Sambat (BS) and Gregorian (AD) calendars.
npm install bs-ad-date-converterYYYY-MM-DD format.
bash
npm install bs-ad-date-converter
`
Usage
$3
`typescript
import {
bsToAd,
adToBs,
ageCalculater,
dateDifference,
getTodayDate
} from 'bs-ad-date-converter';
`
$3
#### Convert BS to AD
`typescript
const adDate = bsToAd('2078-01-01');
console.log(adDate); // Output: "2021-04-14"
`
#### Convert AD to BS
`typescript
const bsDate = adToBs('2021-04-14');
console.log(bsDate); // Output: "2078-01-01"
`
$3
#### Get Current Date
Returns the current date in both AD and BS formats.
`typescript
const { todayAdDate, todayBSDate } = getTodayDate();
console.log(Today: ${todayBSDate} (BS) / ${todayAdDate} (AD));
`
#### Calculate Age
Calculate age from a birth date. You can specify the input date type ('AD' or 'BS').
`typescript
// From AD DOB
const ageAD = ageCalculater('2000-01-01', 'AD');
console.log(ageAD);
// Output: { years: 25, months: 0, days: 8 } (results vary based on current date)
// From BS DOB
const ageBS = ageCalculater('2056-09-17', 'BS');
console.log(ageBS);
`
#### Date Difference
Calculate the number of days between two dates. Both dates must be of the same type ('AD' or 'BS').
`typescript
// Difference between two AD dates
const diffAD = dateDifference('2022-01-01', '2022-01-10', 'AD');
console.log(diffAD); // Output: 9
// Difference between two BS dates
const diffBS = dateDifference('2078-01-01', '2078-01-10', 'BS');
console.log(diffBS); // Output: 9
`
API Reference
$3
Converts a BS date string (YYYY-MM-DD) to AD. Throws an error if invalid.
$3
Converts an AD date string (YYYY-MM-DD) to BS. Throws an error if invalid or out of range.
$3
Returns the current system date converted to both AD and BS strings.
$3
Calculates the age based on the provided date of birth. defaults dateType to 'AD'.
$3
Calculates the absolute difference in days between date1 and date2. defaults dateType to 'AD'.
Supported Date Range
| Calendar | Start Date | End Date |
| -------- | ---------- | ---------- |
| BS | 1970-01-01 | 2099-12-30 |
| AD | 1913-04-13 | 2043-04-13 |
Error Handling
Errors are thrown for invalid formats or dates outside the supported range.
`typescript
try {
bsToAd('2100-01-01');
} catch (e) {
console.error(e.message); // "Invalid BS date" or range error
}
``