A JavaScript library for working with Persian (Jalali) dates, extending the native JavaScript Date object.
npm install @mu-utils/persian-date
import PersianDate from "@mu-utils/persian-date";
const persianDate = new PersianDate();
console.log(persianDate.format("YYYY/MM/DD"));
console.log(persianDate.format("YYYY/MM/DD HH:mm:ss"));
- Supports both Persian (Jalali) and Gregorian calendars
- Extends the native JavaScript Date object
- Flexible date formatting
- Date manipulation (add, subtract)
- Date comparison and difference calculation
- Leap year detection
The PersianDate
constructor supports multiple overloads:
new PersianDate();
new PersianDate(options);
new PersianDate(value, options);
new PersianDate(year, month, options);
new PersianDate(year, month, date, options);
new PersianDate(year, month, date, hours, options);
new PersianDate(year, month, date, hours, minutes, options);
new PersianDate(year, month, date, hours, minutes, seconds, options);
new PersianDate(year, month, date, hours, minutes, seconds, ms, options);
Sets the time zone for the PersianDate instance.
Sets the calendar used by the PersianDate instance.
Formats the current PersianDate instance using the provided date format template.
Calculates the difference between the current PersianDate instance and the provided date value.
Adds the specified time unit and value to the current PersianDate instance.
Subtracts the specified time unit and value from the current PersianDate instance.
Gets the full year of the current PersianDate instance.
Gets the day of the month for the current PersianDate instance.
Gets the month for the current PersianDate instance.
Determines if the current year is a leap year based on the selected calendar.
The conversion from Gregorian to Persian (Jalali) dates is based on an algorithm that involves the following steps:
-
Ephemeris Base Calculation: The base year is adjusted depending on whether the year is negative or positive.
const epbase = year - (year >= 0 ? 474 : 473);
-
Ephemeris Year Calculation: This determines the ephemeris year based on the adjusted base.
const epyear = 474 + mod(epbase, 2820);
-
Day of Year Calculation: The day of the year is computed by considering whether the month is within the first seven months or the latter five.
const dayOfYear = day + (month <= 7 ? (month - 1) * 31 : (month - 1) * 30 + 6);
-
Total Days Calculation: The total number of days is calculated based on the ephemeris year, accounting for leap years.
const yearDays = (epyear - 1) * 365; const leapYears = Math.floor(epbase / 2820) * 1029983; const leapYearDays = Math.floor((epyear * 682 - 110) / 2816);
-
Leap Year Adjustment: The algorithm checks if the current year is a leap year and makes necessary adjustments based on the month.
const isLeap = isPersianLeapYear(year); const extraDay = isLeap && month > 6 ? 1 : 0;
This algorithm ensures accurate conversion between the two calendar systems, handling the complexities of leap years and month lengths.
Create an instance of PersianDate
using the current date:
import PersianDate from "@mu-utils/persian-date";
const currentDate = new PersianDate();
console.log(currentDate.format("YYYY/MM/DD")); // e.g., "1403/07/01"
Create a Persian date using a specific year, month, and day:
const specificDate = new PersianDate(1402, 12, 25);
console.log(specificDate.format("YYYY/MM/DD")); // e.g., "1402/12/25"
Format a Persian date instance using a custom template:
const formattedDate = currentDate.format("YYYY/MM/DD HH:mm:ss");
console.log(formattedDate); // e.g., "1403/07/01 12:34:56"
Add days to the current date:
const futureDate = currentDate.add("days", 10);
console.log(futureDate.format("YYYY/MM/DD")); // e.g., "1403/07/11"
Subtract months from a specific date:
const pastDate = specificDate.subtract("months", 3);
console.log(pastDate.format("YYYY/MM/DD")); // e.g., "1402/09/25"
Calculate the difference in days between two dates:
const anotherDate = new PersianDate(1403, 07, 01);
const daysDifference = anotherDate.diff(specificDate, "days");
console.log(`Difference in days: ${daysDifference}`); // e.g., "Difference in days: 90"
Check if the current year is a leap year:
const isLeap = currentDate.isLeapYear();
console.log(`Is the current year a leap year? ${isLeap}`); // e.g., "Is the current year a leap year? true"