DateFormat.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. class DateFormat {
  2. static const num ONE_MINUTE = 60000;
  3. static const num ONE_HOUR = 3600000;
  4. static const num ONE_DAY = 86400000;
  5. static const num ONE_WEEK = 604800000;
  6. static const String ONE_SECOND_AGO = "秒前";
  7. static const String ONE_MINUTE_AGO = "分钟前";
  8. static const String ONE_HOUR_AGO = "小时前";
  9. static const String ONE_DAY_AGO = "天前";
  10. static const String ONE_MONTH_AGO = "月前";
  11. static const String ONE_YEAR_AGO = "年前";
  12. static String formatCreateAtYYYYMMDD(String? date, {String join = "-"}) {
  13. if (date == null) return '';
  14. var time = DateTime.parse(date);
  15. return "${time.year}$join${time.month}$join${time.day}}";
  16. }
  17. static String formatCreateAtHHmm(String? date) {
  18. if (date == null) return '';
  19. var time = DateTime.parse(date);
  20. return "${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}";
  21. }
  22. static String formatCreateAtYYYYMMDDHHmm(String? date) {
  23. if (date == null) return '';
  24. var time = DateTime.parse(date);
  25. return "${time.year}-${time.month}-${time.day} ${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}";
  26. }
  27. static String formatCreateAt(String? date) {
  28. if (date == null) return '';
  29. return format(DateTime.parse(date));
  30. }
  31. static String format(DateTime? date, {int level = 99}) {
  32. if(date == null)
  33. return "";
  34. return formatTime(date.millisecondsSinceEpoch ~/ 1000, level: level);
  35. }
  36. static String formatTime(num? time, {int level = 99}) {
  37. if(time == null)
  38. return "";
  39. num delta = DateTime.now().millisecondsSinceEpoch - time * 1000;
  40. if (delta <= 1000) {
  41. return "刚刚";
  42. }
  43. if (delta < 1 * ONE_MINUTE && level > 1) {
  44. num seconds = toSeconds(delta);
  45. return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;
  46. }
  47. if (delta < 60 * ONE_MINUTE && level > 2) {
  48. num minutes = toMinutes(delta);
  49. return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO;
  50. }
  51. if (delta < 24 * ONE_HOUR && level > 3) {
  52. num hours = toHours(delta);
  53. return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO;
  54. }
  55. if(level == 99) {
  56. var date = DateTime.fromMillisecondsSinceEpoch(time.toInt() * 1000);
  57. return "${date.year}-${date.month}-${date.day}";
  58. }else if(level == 98) {
  59. var date = DateTime.fromMillisecondsSinceEpoch(time.toInt() * 1000);
  60. return "${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}";
  61. }else {
  62. var date = DateTime.fromMillisecondsSinceEpoch(time.toInt() * 1000);
  63. return "${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}:${date.second}";
  64. }
  65. // if (delta < 48 * ONE_HOUR) {
  66. // return "昨天";
  67. // }
  68. // if (delta < 30 * ONE_DAY) {
  69. // num days = toDays(delta);
  70. // return (days <= 0 ? 1 : days).toInt().toString() + ONE_DAY_AGO;
  71. // }
  72. // if (delta < 12 * 4 * ONE_WEEK) {
  73. // num months = toMonths(delta);
  74. // return (months <= 0 ? 1 : months).toInt().toString() + ONE_MONTH_AGO;
  75. // } else {
  76. // num years = toYears(delta);
  77. // return (years <= 0 ? 1 : years).toInt().toString() + ONE_YEAR_AGO;
  78. // }
  79. }
  80. static num toSeconds(num date) {
  81. return date / 1000;
  82. }
  83. static num toMinutes(num date) {
  84. return toSeconds(date) / 60;
  85. }
  86. static num toHours(num date) {
  87. return toMinutes(date) / 60;
  88. }
  89. static num toDays(num date) {
  90. return toHours(date) / 24;
  91. }
  92. static num toMonths(num date) {
  93. return toDays(date) / 30;
  94. }
  95. static num toYears(num date) {
  96. return toMonths(date) / 365;
  97. }
  98. static String toVideoTime(int second) {
  99. int hour = 0, min = 0, sec = 0, _second = second;
  100. sec = _second % 60;
  101. _second = _second ~/ 60;
  102. min = _second % 60;
  103. hour = _second ~/ 60;
  104. if(hour > 0) {
  105. return "${hour.toString().padLeft(2, "0")}:${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
  106. }else{
  107. return "${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
  108. }
  109. }
  110. static String toHourTime(int second) {
  111. int hour = 0, min = 0, sec = 0, _second = second;
  112. sec = _second % 60;
  113. _second = _second ~/ 60;
  114. min = _second % 60;
  115. hour = _second ~/ 60;
  116. return "${hour.toString().padLeft(2, "0")}:${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
  117. }
  118. static String toTime(int second) {
  119. int hour = 0, min = 0, sec = 0, _second = second;
  120. sec = _second % 60;
  121. _second = _second ~/ 60;
  122. min = _second % 60;
  123. hour = _second ~/ 60;
  124. return "${hour.toString().padLeft(2, "0")}:${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
  125. }
  126. static String toTime1(int second) {
  127. int hour = 0, min = 0, sec = 0, _second = second;
  128. sec = _second % 60;
  129. _second = _second ~/ 60;
  130. min = _second % 60;
  131. hour = _second ~/ 60;
  132. if(hour > 0){
  133. return "${hour.toString().padLeft(2, "0")}:${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
  134. }else{
  135. return "${min.toString().padLeft(2, "0")}:${sec.toString().padLeft(2, "0")}";
  136. }
  137. }
  138. static String toTimeDateFormat(String? time) {
  139. if(time == null)
  140. return "";
  141. var date = DateTime.parse(time);
  142. return "${date.hour.toString().padLeft(2, "0")}:${date.minute.toString().padLeft(2, "0")}";
  143. }
  144. }