date-fns-jalali
date-fns-jalali is a comprehensive JavaScript utility library that adapts the popular `date-fns` toolset for the Jalali (Persian) calendar system. It provides over 200 functions for manipulating, formatting, and comparing dates, all while adhering to `date-fns`'s principles of modularity, immutability, and native Date object usage. The library is currently at version `4.1.0-0` and maintains a release cadence closely tied to its upstream `date-fns` dependency, frequently rebasing to incorporate new features and fixes. Key differentiators include its pure function approach, full TypeScript support, and robust internationalization capabilities, making it a reliable choice for applications requiring Jalali calendar functionality in both browser and Node.js environments.
Common errors
-
TypeError: format is not a function
cause Attempting to use CommonJS `require()` syntax or incorrect named import when the library primarily supports ES Modules, or a bundler issue.fixEnsure you are using `import { format } from 'date-fns-jalali';` for ES Modules. If in a CommonJS environment, verify your build process handles ESM imports correctly or check if a specific CJS export is available (less common for this library). -
RangeError: Invalid time value
cause This error often occurs when date functions receive arguments that do not represent valid date components (e.g., incorrect month or day numbers for the Jalali calendar, or an invalid type passed to `newDate`).fixDouble-check the arguments passed to `newDate` and other date manipulation functions, ensuring they are within the valid ranges for Jalali year, month, and day. Remember that Jalali months are typically 1-12, and days vary by month and leap year. -
ReferenceError: faIR is not defined
cause A locale object (like `faIR` for Farsi/Persian) was used in a formatting function but was not correctly imported or provided to the function's options.fixImport the required locale explicitly using `import faIR from 'date-fns-jalali/locale/fa-IR';` and pass it to functions that accept a `locale` option, e.g., `format(date, 'PPP', { locale: faIR })`.
Warnings
- breaking In version `4.0.0-0`, the logic for weekend and business days was changed. Only Friday is now considered a weekend day, which may affect calculations involving business days or weekend checks.
- gotcha date-fns-jalali frequently rebases to the upstream date-fns library. This means that breaking changes, new features, or behavior shifts introduced in major or minor versions of `date-fns` will propagate to `date-fns-jalali`. Users should consult `date-fns` release notes in addition to `date-fns-jalali`'s.
- gotcha When working with Jalali dates, use `newDate` provided by `date-fns-jalali` instead of the native `new Date()` constructor. Mixing them can lead to unexpected behavior or incorrect calendar calculations.
Install
-
npm install date-fns-jalali -
yarn add date-fns-jalali -
pnpm add date-fns-jalali
Imports
- format
const format = require('date-fns-jalali').format;import { format } from 'date-fns-jalali'; - newDate
import newDate from 'date-fns-jalali/newDate';
import { newDate } from 'date-fns-jalali'; - faIR locale
import { faIR } from 'date-fns-jalali';import faIR from 'date-fns-jalali/locale/fa-IR';
Quickstart
import { compareAsc, format, newDate } from "date-fns-jalali";
// Format a Gregorian Date to Jalali string
const gregorianDate = new Date(2014, 1, 11); // February 11, 2014
console.log(`Gregorian date (${gregorianDate.toLocaleDateString()}) formatted to Jalali: ${format(gregorianDate, "yyyy-MM-dd")}`);
// Expected output: '1392-11-22'
// Create a new Jalali Date object
const jalaliDate = newDate(1392, 10, 22); // Jalali 1392 Bahman 22
console.log(`New Jalali date (1392/10/22): ${jalaliDate.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric'})}`);
// Expected output: a Date object representing Tue Feb 11 2014 00:00:00
// Format the Jalali Date object with a specific Jalali format (assuming locale is applied or inferred)
console.log(`Formatted Jalali date: ${format(jalaliDate, "yyyy MMMM d")}`);
// Expected output: '1392 بهمن 22'
// Sort an array of Gregorian dates using date-fns-jalali's compare function
const dates = [
new Date(1995, 6, 2),
new Date(1987, 1, 11),
new Date(1989, 6, 10),
];
dates.sort(compareAsc);
console.log('Sorted dates:', dates.map(d => d.toLocaleDateString()));
// Expected output: [Feb 11 1987, Jul 10 1989, Jul 02 1995]