formatDate static method

String formatDate(
  1. DateTime? dateTime, {
  2. String? format,
})

format date by DateTime. format 转换格式(已提供常用格式 DateFormats,可以自定义格式:'yyyy/MM/dd HH:mm:ss') 格式要求 year -> yyyy/yy month -> MM/M day -> dd/d hour -> HH/H minute -> mm/m second -> ss/s

Implementation

static String formatDate(DateTime? dateTime, {String? format}) {
  if (dateTime == null) return '';
  format = format ?? DateFormats.full;
  if (format.contains('yy')) {
    String year = dateTime.year.toString();
    if (format.contains('yyyy')) {
      format = format.replaceAll('yyyy', year);
    } else {
      format = format.replaceAll(
          'yy', year.substring(year.length - 2, year.length));
    }
  }

  format = _comFormat(dateTime.month, format, 'M', 'MM');
  format = _comFormat(dateTime.day, format, 'd', 'dd');
  format = _comFormat(dateTime.hour, format, 'H', 'HH');
  format = _comFormat(dateTime.minute, format, 'm', 'mm');
  format = _comFormat(dateTime.second, format, 's', 'ss');
  format = _comFormat(dateTime.millisecond, format, 'S', 'SSS');

  return format;
}