You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

20 lines
604 B

import 'package:intl/intl.dart';
class TimeFormatter {
static String formatTime(DateTime time) {
final now = DateTime.now();
final diff = now.difference(time).inHours;
if (diff < 24) {
return '刚刚'; // 24小时内显示为“刚刚”
} else if (diff < 48) {
return '昨天'; // 昨天
} else if (diff < 72) {
return '前天'; // 前天
} else if (time.year == now.year) {
return DateFormat('MM月dd日').format(time); // 今年内的日期
} else {
return DateFormat('yyyy年MM月dd日').format(time); // 其他年份的日期
}
}
}