TypeScript / JavaScript functions for converting between the Jalaali (Jalali, Persian, Khayyami, Khorshidi, Shamsi) and Gregorian calendar systems.
- Written in TypeScript, with first-class
.d.tstypes - Dual ESM and CommonJS publish via the
exportsfield - Zero runtime dependencies
- Same proven Borkowski algorithm as v1 — bit-for-bit compatible conversions
v2 is a major release. If you are upgrading from v1, read the migration guide in
CHANGELOG.md.
If you just need to display a date and time in the Persian calendar, the
ECMAScript Intl API has excellent browser support and may be
enough:
const d = new Date(2022, 2, 21)
new Intl.DateTimeFormat('fa-IR').format(d)
// => ۱۴۰۱/۱/۱
new Intl.DateTimeFormat('fa-IR', { dateStyle: 'full', timeStyle: 'long' }).format(d)
// => ۱۴۰۱ فروردین ۱, دوشنبه، ساعت ۰:۰۰:۰۰ (+۳:۳۰ گرینویچ)
new Intl.DateTimeFormat('en-US-u-ca-persian', { dateStyle: 'full' }).format(d)
// => Monday, Farvardin 1, 1401 APThe
jalaali-jsalgorithm diverges fromIntlafter Gregorian year 2256 (Jalali 1634) because of how leap years are computed. Inside the 1800–2256 range the two agree exactly. See this comparison.
Reach for jalaali-js when you need to manipulate dates — arithmetic,
validation, week boundaries, Julian Day numbers — or when you target
runtimes (or environments) that ship without Intl.
pnpm add jalaali-js
# or
npm install jalaali-js
# or
yarn add jalaali-jsRequires Node 20 or newer.
import { toJalaali, toGregorian } from 'jalaali-js'
toJalaali(2016, 4, 11) // { jy: 1395, jm: 1, jd: 23 }
toGregorian(1395, 1, 23) // { gy: 2016, gm: 4, gd: 11 }const { toJalaali, toGregorian } = require('jalaali-js')
toJalaali(2016, 4, 11)The npm package ships ESM and CJS; consume it through a CDN that supports ESM imports:
<script type="module">
import { toJalaali } from 'https://esm.sh/jalaali-js'
console.log(toJalaali(2016, 4, 11))
</script>All exports are named and fully typed.
toJalaali(2016, 4, 11) // { jy: 1395, jm: 1, jd: 23 }toJalaali(new Date(2016, 3, 11)) // { jy: 1395, jm: 1, jd: 23 }toGregorian(1395, 1, 23) // { gy: 2016, gm: 4, gd: 11 }isValidJalaaliDate(1394, 12, 30) // false
isValidJalaaliDate(1395, 12, 30) // trueisLeapJalaaliYear(1394) // false
isLeapJalaaliYear(1395) // trueThrows RangeError if jy is outside the supported range
(-61 … 3177).
jalaaliMonthLength(1394, 12) // 29
jalaaliMonthLength(1395, 12) // 30Whether the Jalaali year is leap (leap === 0), the Gregorian year of
its start, and the day in March of Farvardin 1.
jalCal(1390) // { leap: 3, gy: 2011, march: 21 }
jalCal(1391) // { leap: 0, gy: 2012, march: 20 }
jalCal(1395) // { leap: 0, gy: 2016, march: 20 }Faster variant that skips the leap-cycle calculation when you only need
gy and march. New in v2; replaces the v1 jalCal(jy, true)
overload.
jalCalShort(1391) // { gy: 2012, march: 20 }Jalaali date → Julian Day number.
j2d(1395, 1, 23) // 2457490Julian Day number → Jalaali date.
d2j(2457490) // { jy: 1395, jm: 1, jd: 23 }Gregorian date → Julian Day number. Tested good from 1 March, -100100 (of both calendars) up to a few million years into the future.
g2d(2016, 4, 11) // 2457490Julian Day number → Gregorian date. Covers jdn ≥ -34839655
(year -100100 of both calendars).
d2g(2457490) // { gy: 2016, gm: 4, gd: 11 }Convert a Jalaali date (optionally with time) to a JavaScript Date.
jalaaliToDateObject(1400, 4, 30) // new Date(2021, 6, 21)
jalaaliToDateObject(1400, 4, 30, 14, 30) // new Date(2021, 6, 21, 14, 30)Saturday and Friday of the Jalaali week containing the given date. The Jalaali week starts on Saturday.
jalaaliWeek(1400, 4, 30)
// { saturday: { jy: 1400, jm: 4, jd: 26 },
// friday: { jy: 1400, jm: 5, jd: 1 } }pnpm install
pnpm typecheck
pnpm test
pnpm build
pnpm benchThe Jalali calendar is a solar calendar used historically in Persia and still in use today in Iran and Afghanistan. See Wikipedia and the Calendar Converter for background.
Conversions are based on the algorithm by Kazimierz M. Borkowski.
MIT