nepali-date-modern
v0.2.0
Published
A modern, TypeScript-based Nepali date library for Bikram Sambat (B.S.) and Gregorian calendar (A.D.) conversions with comprehensive features
Downloads
2
Maintainers
Readme
Nepali Date Modern
A modern, TypeScript-based Nepali date library for Bikram Sambat (B.S.) and Gregorian calendar (A.D.) conversions with comprehensive features.
Note: This is a modernized fork of the original nepali-date package by Ranjan Shrestha. This version includes TypeScript support, enhanced features, and improved maintainability.
Features
- ✅ TypeScript Support - Full TypeScript support with type definitions
- ✅ Extended Date Range - Support for years 2000-2100 BS
- ✅ Accurate Conversions - Precise Bikram Sambat ↔ Gregorian conversions
- ✅ Holiday Detection - Built-in Nepali holiday detection
- ✅ Rich Formatting - Extensive date formatting options in English and Nepali
- ✅ Date Manipulation - Add/subtract days, months, years
- ✅ Date Comparison - Compare dates, calculate differences
- ✅ Leap Year Support - Proper handling of leap years
- ✅ Validation - Comprehensive input validation
- ✅ Modern Build - ES modules, CommonJS, and UMD support
Installation
npm install nepali-date-modern
# or
yarn add nepali-date-modernQuick Start
import { NepaliDate } from 'nepali-date-modern';
// Create from Nepali date string
const date1 = new NepaliDate('2075-03-22');
console.log(date1.toString()); // "2075/3/22"
// Create from English date
const date2 = new NepaliDate(new Date('2018-07-06'));
console.log(date2.getYear()); // 2075
// Create from components
const date3 = new NepaliDate(2075, 2, 22); // year, month (0-11), day
console.log(date3.getMonth()); // 2 (March)
// Format dates
console.log(date1.format('YYYY-MM-DD')); // "2075-03-22"
console.log(date1.format('yyyy-mm-dd')); // "२०७५-०३-२२"
console.log(date1.format('MMMM D, YYYY')); // "Chaitra 22, 2075"API Reference
Constructor
new NepaliDate() // Current date
new NepaliDate('2075-03-22') // From Nepali date string
new NepaliDate(new Date()) // From English Date object
new NepaliDate(2075, 2, 22) // From year, month, day
new NepaliDate(timestamp) // From timestamp
new NepaliDate(otherNepaliDate) // Clone existing dateDate Conversion
const nepaliDate = new NepaliDate('2075-03-22');
// Get English date
const englishDate = nepaliDate.getEnglishDate();
console.log(englishDate.toDateString()); // "Fri Jul 06 2018"
// Get timestamp
const timestamp = nepaliDate.getTime();Date Components
const date = new NepaliDate('2075-03-22');
console.log(date.getYear()); // 2075
console.log(date.getMonth()); // 2 (March, 0-based)
console.log(date.getDate()); // 22
console.log(date.getDay()); // 5 (Friday, 0-based)
console.log(date.getHours()); // Current hour
console.log(date.getMinutes()); // Current minute
console.log(date.getSeconds()); // Current secondDate Manipulation
const date = new NepaliDate('2075-03-22');
// Add/subtract days
const tomorrow = date.addDays(1);
const yesterday = date.addDays(-1);
// Add/subtract months
const nextMonth = date.addMonths(1);
const prevMonth = date.addMonths(-1);
// Add/subtract years
const nextYear = date.addYears(1);
const prevYear = date.addYears(-1);
// Set components
date.setYear(2076);
date.setMonth(4); // May (0-based)
date.setDate(15);Date Comparison
const date1 = new NepaliDate('2075-03-22');
const date2 = new NepaliDate('2075-03-23');
console.log(date1.isBefore(date2)); // true
console.log(date2.isAfter(date1)); // true
console.log(date1.equals(date2)); // false
// Calculate difference in days
const diff = date2.diffInDays(date1); // 1Formatting
The library supports extensive formatting options:
Year Formats
YYYY- 4 digit year (2075)yyyy- 4 digit year in Nepali (२०७५)YYY- 3 digit year (075)yyy- 3 digit year in Nepali (०७५)YY- 2 digit year (75)yy- 2 digit year in Nepali (७५)
Month Formats
M- month number (1-12)m- month number in Nepali (१-१२)MM- month with zero padding (01-12)mm- month in Nepali with zero padding (०१-१२)MMM- short month name (Bai, Jes, Asa, etc.)mmm- short month name in Nepali (बै, जे, अ, etc.)MMMM- full month name (Baisakh, Jestha, etc.)mmmm- full month name in Nepali (बैसाख, जेष्ठ, etc.)
Day Formats
D- day of month (1-31)d- day of month in Nepali (१-३१)DD- day with zero padding (01-31)dd- day in Nepali with zero padding (०१-३१)DDD- weekday short (Sun, Mon, etc.)ddd- weekday short in Nepali (आइत, सोम, etc.)DDDD- weekday full (Sunday, Monday, etc.)dddd- weekday full in Nepali (आइतबार, सोमबार, etc.)
Examples
const date = new NepaliDate('2075-03-22');
console.log(date.format('YYYY-MM-DD')); // "2075-03-22"
console.log(date.format('yyyy-mm-dd')); // "२०७५-०३-२२"
console.log(date.format('MMMM D, YYYY')); // "Chaitra 22, 2075"
console.log(date.format('mmmm d, yyyy')); // "चैत्र २२, २०७५"
console.log(date.format('DDDD, MMMM D')); // "Friday, Chaitra 22"
console.log(date.format('dddd, mmmm d')); // "शुक्रबार, चैत्र २२"
console.log(date.format('"Today is" DDDD')); // "Today is Friday"Holiday Detection
const date = new NepaliDate('2075-01-01');
// Check if date is a holiday
if (date.isHoliday()) {
console.log('It\'s a holiday!');
}
// Get holiday details
const holidays = date.getHolidays();
holidays.forEach(holiday => {
console.log(`${holiday.name} (${holiday.nameNepali})`);
console.log(`Type: ${holiday.type}`);
});Leap Year Support
const date = new NepaliDate('2076-01-01');
console.log(date.isLeapYear()); // true
const nonLeapDate = new NepaliDate('2075-01-01');
console.log(nonLeapDate.isLeapYear()); // falseStatic Methods
// Create from timestamp
const date1 = NepaliDate.fromTimestamp(Date.now());
// Create from English date string
const date2 = NepaliDate.fromEnglishDate('2018-07-06');
// Get current date
const now = NepaliDate.now();
// Get supported date range
const minDate = NepaliDate.minimum();
const maxDate = NepaliDate.maximum();Validation
The library includes comprehensive validation:
// Valid dates
new NepaliDate('2075-03-22'); // ✅
new NepaliDate('2075/03/22'); // ✅
new NepaliDate('2075.03.22'); // ✅
// Invalid dates (will throw errors)
new NepaliDate('1999-01-01'); // ❌ Year out of range
new NepaliDate('2101-01-01'); // ❌ Year out of range
new NepaliDate('2075-13-01'); // ❌ Invalid month
new NepaliDate('2075-03-32'); // ❌ Invalid day
new NepaliDate('invalid'); // ❌ Invalid formatSupported Date Range
- Nepali Years: 2000 BS to 2100 BS
- Gregorian Years: 1943 AD to 2043 AD
Build Outputs
The library is built with multiple formats:
- CommonJS:
dist/index.js - ES Modules:
dist/index.esm.js - TypeScript Definitions:
dist/index.d.ts
Development
# Install dependencies
npm install
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Lint code
npm run lint
# Build library
npm run build
# Development mode (build + test)
npm run devContributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run tests and linting
- Submit a pull request
License
MIT License - see LICENSE file for details.
Changelog
v0.2.0 - Modernized Fork
- ✨ TypeScript Support - Full TypeScript rewrite with type definitions
- ✨ Extended Date Range - Support for years 2000-2100 BS
- ✨ Holiday Detection - Built-in Nepali holiday detection
- ✨ Rich Formatting - Enhanced formatting options in English and Nepali
- ✨ Date Manipulation - Add/subtract days, months, years
- ✨ Date Comparison - Compare dates, calculate differences
- ✨ Modern Build - ES modules, CommonJS, and UMD support
- 🐛 Bug Fixes - Fixed date calculation issues
- 📚 Documentation - Comprehensive API documentation
- 🔄 Fork - Modernized version of the original nepali-date package
v0.1.3 (Original Package)
- Initial release with basic functionality by Ranjan Shrestha
