formatDate function

String formatDate(
  1. DateTime date,
  2. List<String> formats,
  3. LocaleType locale
)

Implementation

String formatDate(DateTime date, List<String> formats, LocaleType locale) {
  if (formats.first == ymdw) {
    final now = DateTime.now();
    if (date.year == now.year &&
        date.month == now.month &&
        date.day == now.day) {
      //today
      return i18nObjInLocale(locale)?['today'];
    } else if (date.year == now.year) {
      if (locale == LocaleType.zh) {
        return formatDate(date, [_mm, '月', _dd, '日 ', _D], locale);
      } else if (locale == LocaleType.nl) {
        return formatDate(date, [_D, ' ', _dd, ' ', _M], locale);
      } else if (locale == LocaleType.ko) {
        return formatDate(date, [_mm, '월', _dd, '일 ', _D], locale);
      } else if (locale == LocaleType.de) {
        return formatDate(date, [_D, ', ', _dd, '. ', _M], locale);
      } else if (locale == LocaleType.id) {
        return formatDate(date, [_D, ', ', _dd, ' ', _M], locale);
      } else {
        return formatDate(date, [_D, ' ', _M, ' ', _dd], locale);
      }
    } else {
      if (locale == LocaleType.zh) {
        return formatDate(date, [_yyyy, '年', _mm, '月', _dd, '日 ', _D], locale);
      } else if (locale == LocaleType.nl) {
        return formatDate(date, [_D, ' ', _dd, ' ', _M, ' ', _yyyy], locale);
      } else if (locale == LocaleType.ko) {
        return formatDate(date, [_yyyy, '년', _mm, '월', _dd, '일 ', _D], locale);
      } else if (locale == LocaleType.de) {
        return formatDate(date, [_D, ', ', _dd, '. ', _M, ' ', _yyyy], locale);
      } else if (locale == LocaleType.id) {
        return formatDate(date, [_D, ', ', _dd, ' ', _M, ' ', _yyyy], locale);
      } else {
        return formatDate(date, [_D, ' ', _M, ' ', _dd, ', ', _yyyy], locale);
      }
    }
  }

  final sb = new StringBuffer();

  for (String format in formats) {
    if (format == _yyyy) {
      sb.write(digits(date.year, 4));
    } else if (format == _yy) {
      sb.write(digits(date.year % 100, 2));
    } else if (format == _mm) {
      sb.write(digits(date.month, 2));
    } else if (format == _m) {
      sb.write(date.month);
    } else if (format == _MM) {
      String monthLong = i18nObjInLocale(locale)?['monthLong'][date.month - 1];
      sb.write(monthLong);
    } else if (format == _M) {
      String monthShort =
          i18nObjInLocale(locale)?['monthShort'][date.month - 1];
      sb.write(monthShort);
    } else if (format == _dd) {
      sb.write(digits(date.day, 2));
    } else if (format == _d) {
      sb.write(date.day);
    } else if (format == _w) {
      sb.write((date.day + 7) ~/ 7);
    } else if (format == _W) {
      sb.write((dayInYear(date) + 7) ~/ 7);
    } else if (format == _WW) {
      sb.write(digits((dayInYear(date) + 7) ~/ 7, 2));
    } else if (format == _D) {
      String day = i18nObjInLocale(locale)?['day'][date.weekday - 1];
      if (locale == LocaleType.ko) {
        day = "($day)";
      }
      sb.write(day);
    } else if (format == _HH) {
      sb.write(digits(date.hour, 2));
    } else if (format == _H) {
      sb.write(date.hour);
    } else if (format == _hh) {
      sb.write(digits(date.hour % 12, 2));
    } else if (format == _h) {
      sb.write(date.hour % 12);
    } else if (format == _am) {
      sb.write(date.hour < 12
          ? i18nObjInLocale(locale)!['am']
          : i18nObjInLocale(locale)!['pm']);
    } else if (format == _nn) {
      sb.write(digits(date.minute, 2));
    } else if (format == _n) {
      sb.write(date.minute);
    } else if (format == _ss) {
      sb.write(digits(date.second, 2));
    } else if (format == _s) {
      sb.write(date.second);
    } else if (format == _SSS) {
      sb.write(digits(date.millisecond, 3));
    } else if (format == _S) {
      sb.write(date.second);
    } else if (format == _uuu) {
      sb.write(digits(date.microsecond, 2));
    } else if (format == _u) {
      sb.write(date.microsecond);
    } else if (format == _z) {
      if (date.timeZoneOffset.inMinutes == 0) {
        sb.write('Z');
      } else {
        if (date.timeZoneOffset.isNegative) {
          sb.write('-');
          sb.write(digits((-date.timeZoneOffset.inHours) % 24, 2));
          sb.write(digits((-date.timeZoneOffset.inMinutes) % 60, 2));
        } else {
          sb.write('+');
          sb.write(digits(date.timeZoneOffset.inHours % 24, 2));
          sb.write(digits(date.timeZoneOffset.inMinutes % 60, 2));
        }
      }
    } else if (format == Z) {
      sb.write(date.timeZoneName);
    } else {
      sb.write(format);
    }
  }

  return sb.toString();
}