formatDate method

String formatDate(
  1. DateTime date,
  2. bool diffToNow, {
  3. String? format,
})

Method to build a String representing the Date part of a DateTime

Implementation

String formatDate(DateTime date, bool diffToNow, {String? format}) {
  if (!diffToNow) {
    format ??= Strings.get("_date_format_", fallbackValue: null);
  }
  if (format != null) {
    return DateFormat(format).format(date.toLocal());
  }

  if (diffToNow) {
    final now = DateTime.now();
    final date1 = DateTime(date.year, date.month, date.day);
    final date2 = DateTime(now.year, now.month, now.day);
    final diff = date2.difference(date1);
    if (diff.inDays == 0) {
      return Strings.getCapitalized("today");
    } else if (diff.inDays == 1) {
      return Strings.getCapitalized("yesterday");
    } else if (diff.inDays == -1) {
      return Strings.getCapitalized("tomorrow");
    } else if (date1.year == date2.year) {
      return DateFormat(
              Strings.get("_short_date_format_", fallbackValue: null))
          .format(date.toLocal());
    }
  }
  return DateFormat(Strings.get("_date_format_", fallbackValue: null))
      .format(date.toLocal());
}