From e8d9d0ddac94bd90d99a6f5ce430f898da543a6c Mon Sep 17 00:00:00 2001 From: datang Date: Thu, 24 Mar 2022 15:13:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B5=84=E8=AE=AF=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E4=B8=8D=E6=98=BE=E7=A4=BA=E5=85=B7=E4=BD=93=E6=97=B6?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../public_infomation_card.dart | 75 ++++++++++++++++++- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/lib/ui/home/public_infomation/public_infomation_card.dart b/lib/ui/home/public_infomation/public_infomation_card.dart index e70c54c2..eb8e367b 100644 --- a/lib/ui/home/public_infomation/public_infomation_card.dart +++ b/lib/ui/home/public_infomation/public_infomation_card.dart @@ -42,10 +42,7 @@ class PublicInfomationCard extends StatelessWidget { children: [ // Text('测试'), Spacer(), - Text('发布于 ${DateUtil.formatDate( - model.createDateDT, - format: 'yyyy-MM-dd HH:mm', - )}'), + Text('发布于 ${RelativeDateFormat.format(model.createDateDT)}'), ], ), ), @@ -64,3 +61,73 @@ class PublicInfomationCard extends StatelessWidget { ); } } +class RelativeDateFormat { + static final num ONE_MINUTE = 60000; + static final num ONE_HOUR = 3600000; + static final num ONE_DAY = 86400000; + static final num ONE_WEEK = 604800000; + + static final String ONE_SECOND_AGO = "秒前"; + static final String ONE_MINUTE_AGO = "分钟前"; + static final String ONE_HOUR_AGO = "小时前"; + static final String ONE_DAY_AGO = "天前"; + static final String ONE_MONTH_AGO = "月前"; + static final String ONE_YEAR_AGO = "年前"; + +//时间转换 + static String format(DateTime? date) { + num delta = DateTime.now().millisecondsSinceEpoch - date!.millisecondsSinceEpoch; + if (delta < 1 * ONE_MINUTE) { + num seconds = toSeconds(delta); + return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO; + } + if (delta < 60 * ONE_MINUTE) { + num minutes = toMinutes(delta); + return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO; + } + if (delta < 24 * ONE_HOUR) { + num hours = toHours(delta); + return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO; + } + if (delta < 48 * ONE_HOUR) { + return "昨天"; + } + if (delta < 30 * ONE_DAY) { + num days = toDays(delta); + return (days <= 0 ? 1 : days).toInt().toString() + ONE_DAY_AGO; + } + if (delta < 12 * 4 * ONE_WEEK) { + num months = toMonths(delta); + return (months <= 0 ? 1 : months).toInt().toString() + ONE_MONTH_AGO; + } else { + num years = toYears(delta); + return (years <= 0 ? 1 : years).toInt().toString() + ONE_YEAR_AGO; + } + } + + static num toSeconds(num date) { + return date / 1000; + } + + static num toMinutes(num date) { + return toSeconds(date) / 60; + } + + static num toHours(num date) { + return toMinutes(date) / 60; + } + + static num toDays(num date) { + return toHours(date) / 24; + } + + static num toMonths(num date) { + return toDays(date) / 30; + } + + static num toYears(num date) { + return toMonths(date) / 365; + } +} + +