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.
 
 
 
 
 

102 lines
3.5 KiB

import 'package:easy_localization/easy_localization.dart';
extension DateTimeIntExt on int {
String get formatExpired {
if (this == -1) {
return "Perpetually valid".tr();
}
final now = DateTime.now();
final date = DateTime.fromMillisecondsSinceEpoch(this);
final difference = date.difference(now);
if (difference.inHours > 24) {
return "{} Days".tr(args: ["${difference.inDays}"]);
}
if (difference.inHours > 1) {
return "{} Hours".tr(args: ["${difference.inHours}"]);
}
return "<1 Hours".tr();
}
String get formatTimeago {
int value = (this >= 1000000000 && this <= 9999999999) ? this * 1000 : this;
// 将时间戳转换为DateTime对象
final date = DateTime.fromMillisecondsSinceEpoch(value, isUtc: true);
final now = DateTime.now();
final difference = now.difference(date);
if ((date.year - 1) >= now.year) {
return "${date.year}-${date.month}-${date.day}";
} else if (difference.inDays > 1) {
return "${date.month}-${date.day}";
} else if (difference.inDays == 1) {
return 'yesterday'.tr();
} else if (difference.inDays < 1 && difference.inHours >= 3) {
return "${date.hour}:${date.minute}";
} else if (difference.inHours >= 1) {
return '{} hours ago'.tr(args: ["${difference.inHours}"]);
} else if (difference.inMinutes >= 1) {
return '{} minute ago'.tr(args: ["${difference.inMinutes}"]);
} else {
return 'just now'.tr();
}
}
/// hours 时区偏移, 0 为本地时间
String formatString({
String format = "yyyy.MM.dd HH:mm:ss",
int hours = 0,
bool isUtc = false,
String? locale,
}) {
int value = (this >= 1000000000 && this <= 9999999999) ? this * 1000 : this;
// 将时间戳转换为DateTime对象
final date = DateTime.fromMillisecondsSinceEpoch(value,
isUtc: hours > 0 ? true : isUtc);
if (hours != 0) {
Duration utcMinus3Offset = Duration(hours: hours);
return DateFormat(format, locale ?? "en")
.format(date.add(utcMinus3Offset));
}
return DateFormat(format, locale ?? "en").format(date);
}
/// hours 时区偏移, 0 为本地时间
// String formatStringToLoacl(
// {String format = "yyyy-MM-dd HH:mm:ss", int hours = 3}) {
// int value = (this >= 1000000000 && this <= 9999999999) ? this * 1000 : this;
// // 将时间戳转换为DateTime对象
// // final date = DateTime.fromMillisecondsSinceEpoch(value, isUtc: true);
// final date = DateTime.fromMicrosecondsSinceEpoch(value * 1000);
// // if (hours != 0) {
// // Duration utcMinus3Offset = Duration(hours: hours);
// // return DateFormat(format).format(date.add(utcMinus3Offset));
// // }
// return SpUtil().dateFormatOfLocal2(DateFormat(format).format(date));
// }
}
extension DateTimeExt on DateTime {
String get formatUTC3String {
DateFormat dateFormat = DateFormat('yyyy.MM.dd HH:mm:ss', "en");
// 格式化 DateTime 对象
String formattedDate =
dateFormat.format(toUtc().add(const Duration(hours: 3)));
// 返回格式化后的字符串
return '$formattedDate (UTC+3)';
}
String formatStringWithFormat(String fromat) {
DateFormat dateFormat = DateFormat(fromat, "en");
return dateFormat.format(this);
}
}
extension DateTimeStrExt on String {
DateTime get formatUtcTime {
return DateFormat('yyyy-MM-dd HH:mm', "en").parse(this).toUtc();
}
DateTime get formatLocalTime {
return DateFormat('yyyy-MM-dd HH:mm', "en").parse(this).toLocal();
}
}