Converting between Gregorian and Jalali dates
Create PersianDate values from Jalali or Gregorian dates, convert Jalali to Gregorian, compare dates and check leap years and month lengths in Dart.
Creating dates
dart
// From Jalali values. Throws ArgumentError for invalid dates.
final a = PersianDate(1400, 1, 1);
// From the current date and time.
final b = PersianDate.now();
// From a DateTime. The time of day and time zone are kept.
final c = PersianDate.fromDateTime(DateTime.utc(2018, 7, 7, 21, 43, 44));
// From Gregorian values. Out-of-range values roll over like DateTime.
final d = PersianDate.fromGregorian(2021, 3, 21);
// From an ISO 8601 string, parsed with DateTime.parse.
final e = PersianDate.fromGregorianString('2018-07-07T21:43:44.909Z');
print(a.year); // 1400
print(a.month); // 1
print(a.day); // 1Jalali to Gregorian
dart
print(PersianDate(1357, 11, 22).toDateTime()); // 1979-02-11 00:00:00.000A date created from a DateTime returns that exact DateTime, with its time and time zone.
A date created from Jalali values returns local midnight of that day.
Leap years and month lengths
dart
final date = PersianDate(1399, 12, 1);
print(date.isLeapYear); // true
print(date.monthLength); // 30Comparing dates
dart
final last = PersianDate(1399, 12, 30);
final first = PersianDate(1400, 1, 1);
print(last.compareTo(first) < 0); // true
print(first == PersianDate.fromGregorian(2021, 3, 21)); // trueEquality and ordering use the day only; the time of day is ignored.
Supported range
Jalali -61/01/01 to 3177/10/11, which is Gregorian 560-03-20 to 3798-12-31.
Every constructor throws ArgumentError outside that range.