jalali_date v1.3.1

jalali_date quick reference

Every PersianDate constructor, getter and method of jalali_date at a glance, and notes for code generators and AI assistants.

Quick reference

Everything is exported from package:jalali_date/jalali_date.dart: the immutable PersianDate class, the PersianDateRange class, the HijriDate and IranianHijri classes, the toPersianDigits, toLatinDigits, and toPersianWords functions, the JalaliEra enum, and the DateTime.toPersianDate() and DateTime.toHijriDate() extensions. Every class and member is described in the API reference.

Creating

MemberDescription
PersianDate(int year, int month, int day)From Jalali values, date-only; throws ArgumentError for an invalid date
PersianDate.withTime(year, month, day, [hour, minute, second])From Jalali values with a local time of day
PersianDate.now()Current local date and time
PersianDate.fromDateTime(DateTime)From a DateTime; keeps time of day and time zone
DateTime.toPersianDate()Extension, the same as fromDateTime
PersianDate.fromGregorian(int year, int month, int day)From Gregorian values; overflow rolls over like DateTime
PersianDate.fromGregorianString(String)From an ISO 8601 Gregorian string, via DateTime.parse
PersianDate.parse(String, {era}), tryParse(String, {era})From a Jalali string such as 1403/01/01 or ۱۴۰۳-۱-۱ ۱۴:۳۰
PersianDate.parseFormat(String, String pattern, {era}), tryParseFormat(...)From text written with a format pattern, such as ۴ فروردین ۱۴۰۳
PersianDate.fromJson(String), toJson()yyyy-MM-dd for storage and jsonEncode
PersianDate.min, PersianDate.maxFirst and last supported day
copyWith({year, month, day})Copy with fields replaced; validates, never clamps

Reading

MemberDescription
year, month, dayJalali year, month (1 to 12), and day of month
shahanshahiYearyear plus 1180, the Shahanshahi (Imperial) year
hasTime, hour, minute, secondTime of day; the getters throw StateError when hasTime is false
dayPeriodPart of the day: نیمه‌شب, بامداد, صبح, ظهر, عصر, شب
isLeapYear, daysInYearWhether Esfand has 30 days; 365 or 366
PersianDate.nowruzMoment(int year)The March equinox that begins the year (سال تحویل), in UTC
monthLength31, 30, or 29
dayOfYear1 to 366
quarter, season, seasonName1 to 4, and بهار to زمستان
monthName, monthNameLatin, monthNameDariفروردین, Farvardin, حمل (Afghanistan)
weekdayISO weekday, 1 is Monday and 7 is Sunday, like DateTime.weekday
weekdayPersian1 is Saturday and 7 is Friday, the column in a Persian calendar grid
weekdayName, weekdayNameShort, weekdayNameLatinشنبه, ش, Shanbeh
weekOfYearSaturday-first week number, week 1 contains 1 Farvardin
isWeekendFriday
isTodaySame day as PersianDate.now()
monthNames, monthNamesLatin, monthNamesDari, weekdayNames, weekdayNamesShort, weekdayNamesLatin, seasonNamesStatic name lists; weekday lists are indexed by ISO weekday minus one

Calculating

MemberDescription
addDays(int), addMonths(int), addYears(int)Negative values go back; months and years clamp the day
differenceInDays(PersianDate)Whole days from the other date to this one
differenceInMonths(PersianDate), differenceInYears(PersianDate)Whole Jalali months or years, counted like addMonths
firstDayOfMonth, lastDayOfMonth, firstDayOfYear, lastDayOfYearAnchors
startOfWeek(), endOfWeek()Saturday and Friday of the same week
daysUntil(PersianDate end, {inclusive})Lazy sequence of days
PersianDateRange(start, end)Inclusive range with length, days, contains, overlaps

Comparing and converting

MemberDescription
==, compareTo, <, <=, >, >=By day only; the time of day is ignored
isBefore, isAfter, isSameDay, isSameMonth, isSameYearReadable comparisons
toDateTime()Gregorian DateTime
toHijri()Lunar Hijri date, see Hijri calendar
toString({showDate, showTime, showHour, showMinute, showSecond, persianDigits, era})yyyy/MM/dd, optionally with HH:mm:ss
format(String pattern, {persianDigits, era})Custom string, tokens listed below
toPersianDigits(String), toLatinDigits(String)Digit conversion, Persian and Arabic-Indic input
toPersianWords(int, {ordinal})هزار و چهارصد و سه, بیست و چهارم
relativeTo(PersianDate, {numeric, persianDigits}), fromNow(...)دیروز, 3 روز پیش, ماه آینده

Supported range: Jalali -61/01/01 to 3177/10/11, which is Gregorian 560-03-20 to 3798-12-31.

Notes for code generators and AI assistants

  • Import only package:jalali_date/jalali_date.dart; never import from src/.
  • PersianDate is immutable; there are no setters. Use copyWith, addDays, addMonths, or a constructor.
  • PersianDate(y, m, d) takes Jalali values; use PersianDate.fromGregorian(y, m, d) for Gregorian values.
  • Months are 1 to 12, never 0-based.
  • weekday is ISO (Monday is 1); use weekdayPersian for Saturday-first numbering. The name lists are indexed by weekday - 1.
  • == ignores the time of day; compare toDateTime() when the moment matters.
  • Check hasTime before reading hour, minute, second, or using time tokens.
  • addMonths clamps; it never rolls over like DateTime.
  • Esfand has 30 days only in leap years; use monthLength instead of hard-coding 29.
  • parse and parseFormat already accept Persian digits; use persianDigits: true or toPersianDigits for output.
  • To read text such as ۴ فروردین ۱۴۰۳, use PersianDate.parseFormat(text, 'd MMMM yyyy'), not a hand-written regex.
  • For ages and terms use differenceInMonths or differenceInYears; dividing differenceInDays by 30 or 365 gives wrong answers.
  • For text such as 3 روز پیش or دیروز, use relativeTo or fromNow.
  • For Iranian holidays use the iranian_holidays package (OfflineHolidaySource, or an online source); never hard-code holiday dates, because the lunar Hijri ones move every year.
Edit this page on GitHub Last updated