date_extension.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import 'package:get/get.dart';
  2. extension O2DateTimeUtils on DateTime {
  3. /// 当前月份第一天
  4. DateTime monthFirstDate() {
  5. return DateTime(year, month, 1);
  6. }
  7. /// 当前月份最后一天
  8. DateTime monthLastDate() {
  9. return DateTime(year, month + 1, 0);
  10. }
  11. ///
  12. /// 秒数归零
  13. DateTime zeroedSeconds() {
  14. return DateTime(year, month, day, hour, minute, 0);
  15. }
  16. ///
  17. /// 分数归零 包括秒数
  18. DateTime zeroedMinutes({isIncludeSecond = true}) {
  19. return DateTime(year, month, day, hour, 0, isIncludeSecond ? 0 : second);
  20. }
  21. ///
  22. /// 小时数归零 包括秒数
  23. DateTime zeroedHours({isIncludeMinute = true, isIncludeSecond = true}) {
  24. return DateTime(year, month, day, 0, isIncludeMinute ? 0:minute, isIncludeSecond ? 0 : second);
  25. }
  26. ///
  27. /// 增加年数 负数就是减去年数
  28. ///
  29. DateTime addYears(int years) {
  30. final days = 365 * years;
  31. return add(Duration(days: days));
  32. }
  33. ///
  34. /// 增加月数 负数就是减去月数
  35. ///
  36. DateTime addMonths(int months) {
  37. return DateTime(year, month + months, day, hour, minute, second);
  38. }
  39. ///
  40. /// 增加日数 负数就是减去日数
  41. ///
  42. DateTime addDays(int days) {
  43. return add(Duration(days: days));
  44. }
  45. ///
  46. /// 增加小时数 负数就是减去小时数
  47. ///
  48. DateTime addHours(int hours) {
  49. return add(Duration(hours: hours));
  50. }
  51. ///
  52. /// 增加分数 负数就是减去分数
  53. ///
  54. DateTime addMinutes(int minutes) {
  55. return add(Duration(minutes: minutes));
  56. }
  57. String ymd({bool needHyphen = true}) {
  58. var year = this.year;
  59. var month = this.month > 9 ? '${this.month}': '0${this.month}';
  60. var date = day > 9 ? '$day': '0$day';
  61. if (needHyphen) {
  62. return "$year-$month-$date";
  63. }
  64. return "$year$month$date";
  65. }
  66. String ymdhms() {
  67. var year = this.year;
  68. var month = this.month > 9 ? '${this.month}': '0${this.month}';
  69. var date = day > 9 ? '$day': '0$day';
  70. var hour = this.hour > 9 ? '${this.hour}': '0${this.hour}';
  71. var minute = this.minute > 9 ? '${this.minute}': '0${this.minute}';
  72. var second = this.second > 9 ? '${this.second}': '0${this.second}';
  73. return "$year-$month-$date $hour:$minute:$second";
  74. }
  75. String hm() {
  76. var hour = this.hour > 9 ? '${this.hour}': '0${this.hour}';
  77. var minute = this.minute > 9 ? '${this.minute}': '0${this.minute}';
  78. return "$hour:$minute";
  79. }
  80. String hms() {
  81. var hour = this.hour > 9 ? '${this.hour}': '0${this.hour}';
  82. var minute = this.minute > 9 ? '${this.minute}': '0${this.minute}';
  83. var second = this.second > 9 ? '${this.second}': '0${this.second}';
  84. return "$hour:$minute:$second";
  85. }
  86. String friendlyTime() {
  87. var now = DateTime.now();
  88. var minute = now.difference(this).inMinutes;
  89. if (minute < 60) {
  90. return "$minute ${"im_time_minute_before".tr}";
  91. }
  92. var hour = now.difference(this).inHours;
  93. if (hour < 24) {
  94. return "$hour ${"im_time_hour_before".tr}";
  95. }
  96. var days = now.difference(this).inDays;
  97. if (days == 1) {
  98. var yesterday = now.add(const Duration(days: -1));
  99. if (yesterday.day == day) {
  100. return "im_time_yesterday".tr;
  101. }
  102. return "im_time_before_yesterday".tr;
  103. } else if (days == 2) {
  104. var beforeYesterday = now.add(const Duration(days: -2));
  105. if (beforeYesterday.day == day) {
  106. return "im_time_before_yesterday".tr;
  107. }
  108. return "im_time_three_days_ago".tr;
  109. } else if (days < 31) {
  110. return "$days ${"im_time_days_before".tr}";
  111. } else if (days >= 31 && days <= 2 * 31) {
  112. return "im_time_one_month_before".tr;
  113. } else if (days > 2 * 31 && days <= 3 * 31) {
  114. return "im_time_two_month_before".tr;
  115. } else {
  116. return ymd();
  117. }
  118. }
  119. String chatMsgShowTimeFormat() {
  120. var now = DateTime.now();
  121. var hour = this.hour > 9 ? '${this.hour}': '0${this.hour}';
  122. var minute = this.minute > 9 ? '${this.minute}': '0${this.minute}';
  123. // 同一天
  124. if (now.year == year && now.month == month && now.day == day) {
  125. return '$hour:$minute';
  126. } else {
  127. var days = now.difference(this).inDays;
  128. if (days == 1) {
  129. var yesterday = now.add(const Duration(days: -1));
  130. if (yesterday.day == day) {
  131. return "${"im_time_yesterday".tr} $hour:$minute";
  132. }
  133. return "${"im_time_before_yesterday".tr} $hour:$minute";
  134. }
  135. var month = this.month > 9 ? '${this.month}': '0${this.month}';
  136. var date = day > 9 ? '$day': '0$day';
  137. return "$month-$date $hour:$minute";
  138. }
  139. }
  140. }