import 'package:date_format/date_format.dart'; class DateTimeUtils { DateTimeUtils._(); /// 年-月-日 static String? yyyymmdd({ DateTime? date, num? timestamp, }) { var dateTime = getDateTime(date: date, timestamp: timestamp); if (dateTime == null) return null; return formatDate(dateTime, [yyyy, '-', mm, '-', dd]); } /// 年-月-日 时:分:秒 static String? yyyymmddHHnnss({ DateTime? date, num? timestamp, }) { var dateTime = getDateTime(date: date, timestamp: timestamp); if (dateTime == null) return null; return formatDate(dateTime, [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn, ':', ss]); } /// 年-月-日 时:分 static String? yyyymmddHHnn({ DateTime? date, num? timestamp, }) { var dateTime = getDateTime(date: date, timestamp: timestamp); if (dateTime == null) return null; return formatDate(dateTime, [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn]); } /// 时:分:秒 static String? HHnnss({ DateTime? date, num? timestamp, }) { var dateTime = getDateTime(date: date, timestamp: timestamp); if (dateTime == null) return null; return formatDate(dateTime, [HH, ':', nn, ':', ss]); } static DateTime? getDateTime({ DateTime? date, num? timestamp, }) { if (date != null) return date; if (timestamp != null) return DateTime.fromMillisecondsSinceEpoch(timestamp.toInt()); return null; } }