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.
aku_new_community/lib/utils/bee_date_util.dart

52 lines
1.7 KiB

import 'package:flustars/flustars.dart';
class BeeDateUtil {
4 years ago
DateTime? date;
3 years ago
BeeDateUtil(this.date);
3 years ago
DateTime get _now => DateTime.now();
3 years ago
4 years ago
bool get sameYear => _now.year == this.date!.year;
3 years ago
4 years ago
bool get sameMonth => sameYear && _now.month == this.date!.month;
3 years ago
4 years ago
bool get sameDay => sameMonth && _now.day == this.date!.day;
3 years ago
bool get isYesterday =>
(DateTime(_now.year, _now.month, _now.day - 1).compareTo(
4 years ago
DateTime(this.date!.year, this.date!.month, this.date!.day)) ==
0);
3 years ago
bool get isDoubleYesterday =>
(DateTime(_now.year, _now.month, _now.day - 2).compareTo(
4 years ago
DateTime(this.date!.year, this.date!.month, this.date!.day)) ==
0);
String get timeAgo {
4 years ago
Duration duration = _now.difference(date!);
if (duration.inSeconds <= 60) return '${duration.inSeconds}秒前';
if (duration.inMinutes <= 60) return '${duration.inMinutes}分钟前';
if (duration.inHours <= 12) return '${duration.inHours}小时前';
if (isYesterday) return '昨天';
if (isDoubleYesterday) return '前天';
if (duration.inDays <= 30)
return '${duration.inDays}天前';
else
return DateUtil.formatDate(date, format: 'yyyy-MM-dd');
}
3 years ago
String get timeAgoWithHm {
Duration duration = _now.difference(date!);
if (duration.inSeconds <= 60) return '${duration.inSeconds}秒前';
if (duration.inMinutes <= 60) return '${duration.inMinutes}分钟前';
if (duration.inHours <= 12) return '${duration.inHours}小时前';
if (isYesterday) return '昨天';
if (isDoubleYesterday) return '前天';
if (duration.inDays <= 30)
return '${duration.inDays}天前';
else
return DateUtil.formatDate(date, format: 'yyyy-MM-dd HH-mm');
}
}