format static method

String format(
  1. DateTime date, {
  2. String? locale,
  3. DateTime? now,
  4. Duration? max = const Duration(days: 14),
  5. bool short = false,
  6. bool withFuture = false,
})

Formats provided date to a fuzzy time like 'a moment ago'

  • If locale is passed will look for message for that locale, if you want to add or override locales use setLocaleMessages. Defaults to 'en'
  • If now is passed this will be the point of reference for calculating the elapsed time. Defaults to DateTime.now()
  • If max is passed, format will only happen between that duration and beyond that simple date format will be shown. To avoid format limits, Pass null. Defaults to 14 days
  • If short is passed, format will use the short version of locale. i.e. for en_short locale 5 minute ago will format as 5m
  • If withFuture is passed, format will use the From prefix, ie. a date 5 minutes from now in 'en' locale will display as "5 minutes from now"

Implementation

static String format(
  DateTime date, {
  String? locale,
  DateTime? now,
  Duration? max = const Duration(days: 14),
  bool short = false,
  bool withFuture = false,
}) =>
    max != null && (now ?? Date.now).difference(date) > max
        ? short
            ? date.formatMMMdY
            : date.formatMMMMdY
        : timeago.format(
            date,
            locale: (locale ?? _default).post("_short", doIf: short),
            clock: now,
            allowFromNow: withFuture,
          );