date_to_string_converter.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. class DateToStringConverter {
  2. static String dateAndTimeToString(
  3. var timestamp, Map<String, String> formart) {
  4. if (timestamp == null || timestamp == "") {
  5. return "";
  6. }
  7. String targetString = "";
  8. final date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);
  9. // final String tmp = date.toString();
  10. String year = date.year.toString();
  11. String month = date.month.toString();
  12. if (date.month <= 9) {
  13. month = "0" + month;
  14. }
  15. String day = date.day.toString();
  16. if (date.day <= 9) {
  17. day = "0" + day;
  18. }
  19. String hour = date.hour.toString();
  20. if (date.hour <= 9) {
  21. hour = "0" + hour;
  22. }
  23. String minute = date.minute.toString();
  24. if (date.minute <= 9) {
  25. minute = "0" + minute;
  26. }
  27. String second = date.second.toString();
  28. if (date.second <= 9) {
  29. second = "0" + second;
  30. }
  31. // String millisecond = date.millisecond.toString();
  32. String morningOrafternoon = "上午";
  33. if (date.hour >= 12) {
  34. morningOrafternoon = "下午";
  35. }
  36. if (formart["y-m"] != null && formart["m-d"] != null) {
  37. targetString = year + formart["y-m"]! + month + formart["m-d"]! + day;
  38. } else if (formart["y-m"] == null && formart["m-d"] != null) {
  39. targetString = month + formart["m-d"]! + day;
  40. } else if (formart["y-m"] != null && formart["m-d"] == null) {
  41. targetString = year + formart["y-m"]! + month;
  42. }
  43. targetString += " ";
  44. if (formart["m-a"] != null) {
  45. targetString += morningOrafternoon + " ";
  46. }
  47. if (formart["h-m"] != null && formart["m-s"] != null) {
  48. targetString +=
  49. hour + formart["h-m"]! + minute + formart["m-s"]! + second;
  50. } else if (formart["h-m"] == null && formart["m-s"] != null) {
  51. targetString += minute + formart["m-s"]! + second;
  52. } else if (formart["h-m"] != null && formart["m-s"] == null) {
  53. targetString += hour + formart["h-m"]! + minute;
  54. }
  55. return targetString;
  56. }
  57. }