A modern and lightweight NPM package for rendering and converting between Gregorian (AD) and Bikram Sambat (BS) calendars, with full support for Nepali and English localization.
npm install nepali-date-calendarbash
npm install nepali-date-calendar
`
Quick Start
`typescript
import { adToBs, bsToAd, getMonthCalendar, getLocalizedMonthName } from 'nepali-date-calendar';
// Convert AD to BS
const bsDate = adToBs({ year: 2024, month: 1, day: 15 });
console.log(bsDate); // { year: 2080, month: 10, day: 2 }
// Convert BS to AD
const adDate = bsToAd({ year: 2080, month: 10, day: 2 });
console.log(adDate); // { year: 2024, month: 1, day: 15 }
// Get localized month name
const monthName = getLocalizedMonthName(1, 'np'); // 'बैशाख'
const monthNameEn = getLocalizedMonthName(1, 'en'); // 'Baishakh'
`
API Reference
$3
#### adToBs(adDate: AdDate): BsDate
Converts a Gregorian (AD) date to Bikram Sambat (BS) date.
`typescript
interface AdDate {
year: number;
month: number;
day: number;
}
interface BsDate {
year: number;
month: number;
day: number;
}
`
Example:
`typescript
const bsDate = adToBs({ year: 2024, month: 1, day: 15 });
// Returns: { year: 2080, month: 10, day: 2 }
`
#### bsToAd(bsDate: BsDate): AdDate
Converts a Bikram Sambat (BS) date to Gregorian (AD) date.
Example:
`typescript
const adDate = bsToAd({ year: 2080, month: 10, day: 2 });
// Returns: { year: 2024, month: 1, day: 15 }
`
$3
#### getMonthCalendar(date: AdDate | BsDate, type: 'AD' | 'BS'): CalendarCell[][]
Generates a calendar grid for the specified month.
`typescript
type CalendarCell = {
date: AdDate | BsDate;
isCurrentMonth: boolean;
isToday: boolean;
events?: string[];
};
`
Example:
`typescript
const calendar = getMonthCalendar({ year: 2080, month: 10, day: 1 }, 'BS');
// Returns a 2D array representing the calendar grid
`
#### getNextMonth(date: AdDate | BsDate, type: 'AD' | 'BS'): AdDate | BsDate
Gets the next month date.
Example:
`typescript
const nextMonth = getNextMonth({ year: 2080, month: 10, day: 1 }, 'BS');
// Returns: { year: 2080, month: 11, day: 1 }
`
#### getPrevMonth(date: AdDate | BsDate, type: 'AD' | 'BS'): AdDate | BsDate
Gets the previous month date.
Example:
`typescript
const prevMonth = getPrevMonth({ year: 2080, month: 10, day: 1 }, 'BS');
// Returns: { year: 2080, month: 9, day: 1 }
`
$3
#### getLocalizedMonthName(month: number, lang: 'en' | 'np'): string
Gets the localized month name.
Example:
`typescript
getLocalizedMonthName(1, 'np'); // 'बैशाख'
getLocalizedMonthName(1, 'en'); // 'Baishakh'
`
#### getLocalizedWeekdayName(weekday: number, lang: 'en' | 'np'): string
Gets the localized weekday name.
Example:
`typescript
getLocalizedWeekdayName(0, 'np'); // 'आइतबार'
getLocalizedWeekdayName(0, 'en'); // 'Sunday'
`
Usage Examples
$3
`typescript
import React, { useState } from 'react';
import {
getMonthCalendar,
getLocalizedMonthName,
getLocalizedWeekdayName,
getNextMonth,
getPrevMonth
} from 'nepali-date-calendar';
function NepaliCalendar() {
const [currentDate, setCurrentDate] = useState({ year: 2080, month: 10, day: 1 });
const calendar = getMonthCalendar(currentDate, 'BS');
const handleNextMonth = () => {
setCurrentDate(getNextMonth(currentDate, 'BS') as BsDate);
};
const handlePrevMonth = () => {
setCurrentDate(getPrevMonth(currentDate, 'BS') as BsDate);
};
return (
{getLocalizedMonthName(currentDate.month, 'np')} {currentDate.year}
{['आइतबार', 'सोमबार', 'मंगलबार', 'बुधबार', 'बिहीबार', 'शुक्रबार', 'शनिबार'].map(day => (
{day}
))}
{calendar.map((week, weekIndex) =>
week.map((cell, dayIndex) => (
key={${weekIndex}-${dayIndex}}
className={calendar-day ${cell.isToday ? 'today' : ''} ${!cell.isCurrentMonth ? 'other-month' : ''}}
>
{cell.date.day}
))
)}
$3
`typescript
import { adToBs, bsToAd } from 'nepali-date-calendar';
// Convert today's date to BS
const today = new Date();
const todayBs = adToBs({
year: today.getFullYear(),
month: today.getMonth() + 1,
day: today.getDate()
});
console.log(Today in BS: ${todayBs.year}/${todayBs.month}/${todayBs.day});
// Convert BS date to AD
const bsDate = { year: 2080, month: 10, day: 2 };
const adDate = bsToAd(bsDate);
console.log(BS ${bsDate.year}/${bsDate.month}/${bsDate.day} = AD ${adDate.year}/${adDate.month}/${adDate.day});
`
Development
$3
`bash
npm run build
`
$3
`bash
npm test
`
Contributing
1. Fork the repository
2. Create your feature branch (git checkout -b feature/amazing-feature)
3. Commit your changes (git commit -m 'Add some amazing feature')
4. Push to the branch (git push origin feature/amazing-feature`)