date-fns-jalali

4.1.0-0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic date formatting from Gregorian to Jalali, creating a new Jalali date object, and sorting dates using `date-fns-jalali`'s core functions.

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]

view raw JSON →