format method

String format([
  1. DateTimeDist? dist,
  2. bool dual = true
])

转换指定长度的字符串

Implementation

String format([DateTimeDist? dist, bool dual = true]) {
  final DateTime date = this;
  dist ??= DateTimeDist.yearSecond;
  final String year = date.year.toString();
  final String month =
      dual ? date.month.padLeft(2, '0') : date.month.toString();
  final String day = dual ? date.day.padLeft(2, '0') : date.day.toString();
  final String hour = dual ? date.hour.padLeft(2, '0') : date.hour.toString();
  final String minute =
      dual ? date.minute.padLeft(2, '0') : date.minute.toString();
  final String second =
      dual ? date.second.padLeft(2, '0') : date.second.toString();
  final String millisecond =
      dual ? date.millisecond.padLeft(2, '0') : date.millisecond.toString();
  // final String microsecond =
  //     dual ? date.microsecond.padLeft(2, '0') : date.microsecond.toString();
  switch (dist) {
    case DateTimeDist.yearMillisecond:
      return '$year-$month-$day $hour:$minute:$second.$millisecond';
    case DateTimeDist.yearSecond:
      return '$year-$month-$day $hour:$minute:$second';
    case DateTimeDist.yearMinute:
      return '$year-$month-$day $hour:$minute';
    case DateTimeDist.yearHour:
      return '$year-$month-$day $hour';
    case DateTimeDist.yearDay:
      return '$year-$month-$day';
    case DateTimeDist.yearMonth:
      return '$year-$month';
    case DateTimeDist.monthMillisecond:
      return '$month-$day $hour:$minute:$second.$millisecond';
    case DateTimeDist.monthSecond:
      return '$month-$day $hour:$minute:$second';
    case DateTimeDist.monthMinute:
      return '$month-$day $hour:$minute';
    case DateTimeDist.monthHour:
      return '$month-$day $hour';
    case DateTimeDist.monthDay:
      return '$month-$day';
    case DateTimeDist.dayMillisecond:
      return '$day $hour:$minute:$second.$millisecond';
    case DateTimeDist.daySecond:
      return '$day $hour:$minute:$second';
    case DateTimeDist.dayMinute:
      return '$day $hour:$minute';
    case DateTimeDist.dayHour:
      return '$day $hour';
    case DateTimeDist.hourMillisecond:
      return '$hour:$minute:$second.$millisecond';
    case DateTimeDist.hourSecond:
      return '$hour:$minute:$second';
    case DateTimeDist.hourMinute:
      return '$hour:$minute';
  }
}