getTimeDifferenceDescription method
时间戳(Milliseconds)距离当前的时间描述
Implementation
String? getTimeDifferenceDescription({bool isSecond = false}) {
if (this == null) {
return null;
}
int milliseconds = this!.toInt();
if (isSecond) {
milliseconds = milliseconds * 1000;
}
DateTime currentTime = DateTime.now();
DateTime inputTime = DateTime.fromMillisecondsSinceEpoch(milliseconds);
Duration difference = currentTime.difference(inputTime);
if (difference.inSeconds < 60) {
return '刚刚';
} else if (difference.inMinutes < 60) {
return '${difference.inMinutes}分钟前';
} else if (difference.inHours < 24) {
return '${difference.inHours}小时前';
} else if (difference.inDays < 30) {
return '${difference.inDays}天前';
} else if (difference.inDays < 365) {
int months = currentTime.month - inputTime.month;
if (months == 0) {
return '1个月前';
} else {
return '$months个月前';
}
} else {
int years = currentTime.year - inputTime.year;
if (years == 1) {
return '1年前';
} else {
return '$years年前';
}
}
}