更新分享
This commit is contained in:
@@ -118,6 +118,57 @@ class MyUtils {
|
||||
curve: Curves.easeInOut,
|
||||
);
|
||||
}
|
||||
|
||||
static String formatBindTime(DateTime d) {
|
||||
final DateFormat formatter = DateFormat('yyyy/MM/dd');
|
||||
return formatter.format(d);
|
||||
}
|
||||
|
||||
static DateTime? formatBirthdayTime(String? device) {
|
||||
if (device == null || device.isEmpty) return null;
|
||||
|
||||
try {
|
||||
return DateTime.parse(device.replaceAll('/', '-')); // 替换为标准格式
|
||||
} catch (e) {
|
||||
return null; // 解析失败时返回 null
|
||||
}
|
||||
}
|
||||
|
||||
static int getAgeByDate(DateTime? formatBirthdayTime) {
|
||||
if (formatBirthdayTime == null) return 0;
|
||||
|
||||
final now = DateTime.now();
|
||||
int age = now.year - formatBirthdayTime.year;
|
||||
|
||||
// 如果还没到今年生日,减一岁
|
||||
if (now.month < formatBirthdayTime.month ||
|
||||
(now.month == formatBirthdayTime.month &&
|
||||
now.day < formatBirthdayTime.day)) {
|
||||
age--;
|
||||
}
|
||||
|
||||
return age;
|
||||
}
|
||||
|
||||
static String formatDateTimeWeek(DateTime date) {
|
||||
DateTime now = DateTime.now();
|
||||
// 去除时间部分,仅比较年月日
|
||||
DateTime today = DateTime(now.year, now.month, now.day);
|
||||
DateTime target = DateTime(date.year, date.month, date.day);
|
||||
|
||||
if (target == today) {
|
||||
return '今日';
|
||||
}
|
||||
|
||||
const List<String> weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
|
||||
return weekdays[date.weekday % 7]; // Dart中星期日是7,要映射到索引0
|
||||
}
|
||||
|
||||
/// 返回 MM/dd 格式
|
||||
static String formatDateTimeDay(DateTime date) {
|
||||
String twoDigits(int n) => n.toString().padLeft(2, '0');
|
||||
return '${twoDigits(date.month)}/${twoDigits(date.day)}';
|
||||
}
|
||||
}
|
||||
|
||||
Color stringToColor(String hexColor) {
|
||||
|
||||
Reference in New Issue
Block a user