1.0.1 • Published 4 months ago
hijrah-date-ts v1.0.1
hijrah-date-ts
TypeScript implementation of the hijrah-date library with full backward compatibility.
Installation
npm install hijrah-date-ts
Usage
import HijrahDate, { HijriMonth } from 'hijrah-date-ts';
// Create a new Hijri date
const hijriDate = new HijrahDate(1444, HijriMonth.RAMADAN, 15);
// Convert to Gregorian
const gregorianDate = hijriDate.toGregorian();
console.log(gregorianDate); // JS Date object
// Get today's date in Hijri calendar
const today = HijrahDate.now();
console.log(today.format('yyyy-MM-dd')); // e.g., 1444-09-15
// Format a date
console.log(hijriDate.format('dd/MM/yyyy')); // 15/09/1444
// Parse a date string
const parsedDate = HijrahDate.parse('1444-09-15');
// Add days, months, or years
const nextMonth = hijriDate.plusMonths(1);
const nextYear = hijriDate.plusYears(1);
const tenDaysLater = hijriDate.plusDays(10);
// Compare dates
if (hijriDate.isBefore(nextMonth)) {
console.log('hijriDate is before nextMonth');
}
// Get month name
console.log(hijriDate.getMonthName()); // رمضان
console.log(hijriDate.getMonthNameEn()); // Ramadan
// Register adjustments (for different calculation methods)
HijrahDate.registerAdjustments([
{ month: 8, days: 1 } // Adjust Ramadan by +1 day
]);
API Reference
Class: HijrahDate
Static Methods
now()
: Returns current date as HijrahDatefromGregorian(date: Date)
: Converts Gregorian date to HijrahDateparse(dateStr: string)
: Parses string in format "yyyy-MM-dd" to HijrahDatedaysInMonth(year: number, month: number)
: Returns days in specific Hijri monthisLeapYear(year: number)
: Checks if year is a Hijri leap yearregisterAdjustments(adjustments: AdjustmentOptions[])
: Registers custom adjustmentsdaysBetween(start: HijrahDate, end: HijrahDate)
: Calculates days between two dates
Instance Methods
toGregorian()
: Converts to JavaScript Date objectformat(format?: string)
: Formats date with optional patterngetFullYear()
: Returns Hijri yeargetMonth()
: Returns Hijri month (0-based)getDate()
: Returns day of monthgetDay()
: Returns day of week (0 = Sunday)plusDays(days: number)
: Adds days and returns new dateplusMonths(months: number)
: Adds months and returns new dateplusYears(years: number)
: Adds years and returns new datecompareTo(other: HijrahDate)
: Compares with another dateequals(other: HijrahDate)
: Checks if dates are equalisBefore(other: HijrahDate)
: Checks if this date is before otherisAfter(other: HijrahDate)
: Checks if this date is after othertoString()
: Returns string representationendOfMonth()
: Returns date representing last day of monthstartOfMonth()
: Returns date representing first day of monthgetMonthName()
: Returns Arabic name of monthgetMonthNameEn()
: Returns English name of monthgetDayName()
: Returns Arabic name of day of weekgetDayNameEn()
: Returns English name of day of week
Enum: HijriMonth
Constants for Hijri months (0-based):
MUHARRAM
: 0SAFAR
: 1RABI_AL_AWWAL
: 2RABI_AL_THANI
: 3JUMADA_AL_AWWAL
: 4JUMADA_AL_THANI
: 5RAJAB
: 6SHABAN
: 7RAMADAN
: 8SHAWWAL
: 9DHU_AL_QIDAH
: 10DHU_AL_HIJJAH
: 11
Interface: AdjustmentOptions
days?: number
: Days adjustment (positive or negative)month?: number
: Specific month to adjust (0-based)year?: number
: Specific year to adjust
IMPORTANT:
npm run test
yields 1 failed test (as of 17th Mar 2025):
Any contributions appreciated 🙏🏻🙇🏻
License
MIT