/*
 * @Author: XianKaiQun
 * @Date: 2020-07-20 16:14:02
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-11-24 11:15:01
 * @Description: 
 */
class WisDateTimeFormat {
  WisDateTimeFormat._();
  static const String dateAndTime = '{YYYY}-{MM}-{DD} {hh}:{mm}:{ss}';
  static const String date = '{YYYY}-{MM}-{DD}';
  static const String time = '{hh}:{mm}:{ss}';
  static const String yearAndMonth = '{YYYY}-{MM}';
}

///时间格式化
extension ExtensionDateTime on DateTime {
  /// 格式化
  /// format='{YYYY}-{MM}-{DD} {hh}:{mm}:{ss}';
  String toFormat([String format = WisDateTimeFormat.dateAndTime]) {
    final dateTime = this.toLocal();

    String _month = (dateTime.month < 10 ? '0' : '') + '${dateTime.month}';
    String _day = (dateTime.day < 10 ? '0' : '') + '${dateTime.day}';
    String _hour = (dateTime.hour < 10 ? '0' : '') + '${dateTime.hour}';
    String _minute = (dateTime.minute < 10 ? '0' : '') + '${dateTime.minute}';
    String _second = (dateTime.second < 10 ? '0' : '') + '${dateTime.second}';

    return format
        .replaceAll(RegExp(r'{YYYY}'), '${dateTime.year}')
        .replaceAll(RegExp(r'{MM}'), '$_month')
        .replaceAll(RegExp(r'{DD}'), '$_day')
        .replaceAll(RegExp(r'{hh}'), '$_hour')
        .replaceAll(RegExp(r'{mm}'), '$_minute')
        .replaceAll(RegExp(r'{ss}'), '$_second');
  }
}
