mod: hotPage开发

This commit is contained in:
guozhigq
2023-04-19 13:51:48 +08:00
parent 9bac8553a5
commit 40b88eeeb2
6 changed files with 394 additions and 6 deletions

View File

@ -38,4 +38,96 @@ class Utils {
return '';
}
}
// 时间显示刚刚x分钟前
static String dateFormat(timeStamp, {formatType = 'list'}) {
// 当前时间
int time = (DateTime.now().millisecondsSinceEpoch / 1000).round();
// 对比
int _distance = (time - timeStamp).toInt();
// 当前年日期
String currentYearStr = 'MM月DD日 hh:mm';
String lastYearStr = 'YY年MM月DD日 hh:mm';
if (formatType == 'detail') {
currentYearStr = 'MM-DD hh:mm';
lastYearStr = 'YY-MM-DD hh:mm';
return CustomStamp_str(
timestamp: timeStamp,
date: lastYearStr,
toInt: false,
formatType: formatType);
}
if (_distance <= 60) {
return '刚刚';
} else if (_distance <= 3600) {
return '${(_distance / 60).floor()}分钟前';
} else if (_distance <= 43200) {
return '${(_distance / 60 / 60).floor()}小时前';
} else if (DateTime.fromMillisecondsSinceEpoch(time * 1000).year ==
DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000).year) {
return CustomStamp_str(
timestamp: timeStamp,
date: currentYearStr,
toInt: false,
formatType: formatType);
} else {
return CustomStamp_str(
timestamp: timeStamp,
date: lastYearStr,
toInt: false,
formatType: formatType);
}
}
// 时间戳转时间
static String CustomStamp_str(
{int? timestamp, // 为空则显示当前时间
String? date, // 显示格式,比如:'YY年MM月DD日 hh:mm:ss'
bool toInt = true, // 去除0开头
String? formatType}) {
timestamp ??= (DateTime.now().millisecondsSinceEpoch / 1000).round();
String timeStr =
(DateTime.fromMillisecondsSinceEpoch(timestamp * 1000)).toString();
dynamic dateArr = timeStr.split(' ')[0];
dynamic timeArr = timeStr.split(' ')[1];
String YY = dateArr.split('-')[0];
String MM = dateArr.split('-')[1];
String DD = dateArr.split('-')[2];
String hh = timeArr.split(':')[0];
String mm = timeArr.split(':')[1];
String ss = timeArr.split(':')[2];
ss = ss.split('.')[0];
// 去除0开头
if (toInt) {
MM = (int.parse(MM)).toString();
DD = (int.parse(DD)).toString();
hh = (int.parse(hh)).toString();
mm = (int.parse(mm)).toString();
}
if (date == null) {
return timeStr;
}
if (formatType == 'list' && int.parse(DD) > DateTime.now().day - 2) {
return '昨天';
}
date = date
.replaceAll('YY', YY)
.replaceAll('MM', MM)
.replaceAll('DD', DD)
.replaceAll('hh', hh)
.replaceAll('mm', mm)
.replaceAll('ss', ss);
if (int.parse(DD) < DateTime.now().day) {
return date.split(' ')[0];
}
return date;
}
}