date_time_utils.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import 'package:date_format/date_format.dart';
  2. class DateTimeUtils {
  3. DateTimeUtils._();
  4. /// 年-月-日
  5. static String? yyyymmdd({
  6. DateTime? date,
  7. num? timestamp,
  8. }) {
  9. var dateTime = getDateTime(date: date, timestamp: timestamp);
  10. if (dateTime == null) return null;
  11. return formatDate(dateTime, [yyyy, '-', mm, '-', dd]);
  12. }
  13. /// 年-月-日 时:分:秒
  14. static String? yyyymmddHHnnss({
  15. DateTime? date,
  16. num? timestamp,
  17. }) {
  18. var dateTime = getDateTime(date: date, timestamp: timestamp);
  19. if (dateTime == null) return null;
  20. return formatDate(dateTime, [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn, ':', ss]);
  21. }
  22. /// 年-月-日 时:分
  23. static String? yyyymmddHHnn({
  24. DateTime? date,
  25. num? timestamp,
  26. }) {
  27. var dateTime = getDateTime(date: date, timestamp: timestamp);
  28. if (dateTime == null) return null;
  29. return formatDate(dateTime, [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn]);
  30. }
  31. /// 时:分:秒
  32. static String? HHnnss({
  33. DateTime? date,
  34. num? timestamp,
  35. }) {
  36. var dateTime = getDateTime(date: date, timestamp: timestamp);
  37. if (dateTime == null) return null;
  38. return formatDate(dateTime, [HH, ':', nn, ':', ss]);
  39. }
  40. static DateTime? getDateTime({
  41. DateTime? date,
  42. num? timestamp,
  43. }) {
  44. if (date != null) return date;
  45. if (timestamp != null) return DateTime.fromMillisecondsSinceEpoch(timestamp.toInt());
  46. return null;
  47. }
  48. }