formatFull function

String formatFull(
  1. DateTime date, {
  2. String locale = 'en',
  3. DateTime? clock,
  4. bool enableFromNow = false,
})

Formats provided date to a full fuzzy time like '2 years, 9 months, 9 days, 6 hours, 8 minutes, 3 seconds'

  • If locale is passed, will look for message for that locale, if you want to add or override locales use setLocale. Defaults to 'en'
  • If clock is passed, this will be the point of reference for calculating the delta time. Defaults to DateTime.now()
  • If enableFromNow is passed, format will use the From prefix, ie. a date 9 minutes from now in 'en' locale will display as "9 minutes from now"

Implementation

String formatFull(
  DateTime date, {
  String locale = 'en',
  DateTime? clock,
  bool enableFromNow = false,
}) {
  final language = _languages[locale] ?? English();
  clock ??= DateTime.now();

  final Duration duration = clock.difference(date);
  final int seconds = duration.inSeconds % 60;

  if (duration.inSeconds < 1) {
    return language.aboutASecond(0);
  }

  final int minutes = duration.inMinutes % 60;
  final int hours = duration.inHours % 24;
  final int days = duration.inDays % 30;
  final int monthsAll = (duration.inDays / 30).floor();
  final int months = monthsAll % 12;
  final int years = (monthsAll / 12).floor();

  final stringParts = <String>[];

  if (years > 0) {
    stringParts.add(language.years(years));
  }

  if (months > 0) {
    stringParts.add(language.months(months));
  }

  if (days > 0) {
    stringParts.add(language.days(days));
  }

  if (hours > 0) {
    stringParts.add(language.hours(hours));
  }

  if (minutes > 0) {
    stringParts.add(language.minutes(minutes));
  }

  if (seconds > 0) {
    stringParts.add(language.seconds(seconds));
  }

  return stringParts.join(', ');
}