jalali_date v1.0.0

Formatting Jalali dates

Format Persian dates in Dart with toString flags and format() patterns, using Persian month names (فروردین…) and weekday names.

Persian names

dart
final date = PersianDate.fromGregorian(1979, 2, 11);
print(date.monthName); // بهمن
print(date.weekday); // 7 (DateTime.sunday)
print(date.weekdayName); // یکشنبه

weekday follows DateTime.weekday (1 is Monday, 7 is Sunday), even though the Persian week starts on Saturday. PersianDate.monthNames and PersianDate.weekdayNames hold the full lists.

toString

dart
final date = PersianDate.fromGregorianString('2018-07-07T21:43:44.909Z');
print(date.toString()); // 1397/04/16
print(date.toString(showTime: true)); // 1397/04/16 21:43:44
print(date.toString(showTime: true, showSecond: false)); // 1397/04/16 21:43
print(date.toString(showDate: false, showTime: true)); // 21:43:44

The flags are showDate, showTime, showHour, showMinute, and showSecond. Every part is zero-padded to two digits. Hiding everything throws ArgumentError. Showing the time of a date created from Jalali values throws StateError, because it has no time of day.

format

dart
final date = PersianDate.fromGregorian(2021, 3, 21);
print(date.format('d MMMM yyyy')); // 1 فروردین 1400
print(date.format('EEEE yyyy/MM/dd')); // یکشنبه 1400/01/01
TokenOutput
yyyyyear, at least four digits
yylast two digits of the year
MMMMPersian month name
MMmonth, two digits
Mmonth
ddday, two digits
dday
EEEEPersian weekday name
HHhour (0 to 23), two digits
Hhour (0 to 23)
mmminute, two digits
mminute
sssecond, two digits
ssecond

The longest token wins, and every other character is copied as is. There is no escaping, so letters that form a token are always replaced: format('Month') gives 1onth. Time tokens throw StateError for a date created from Jalali values.

In fromJson

dart
class Item {
  final String text;
  final String? date;

  Item(this.text, this.date);

  factory Item.fromJson(Map<String, dynamic> json) {
    final rawDate = json['date'] as String?;
    return Item(
      json['text'] as String,
      rawDate == null
          ? null
          : PersianDate.fromDateTime(DateTime.parse(rawDate).toLocal())
              .format('yyyy/MM/dd HH:mm'),
    );
  }
}
Edit this page on GitHub Last updated