2024-03-19 11:09:07 +08:00
|
|
|
import 'package:date_format/date_format.dart';
|
2024-04-08 13:50:23 +08:00
|
|
|
import 'package:timeago/timeago.dart' as timeago;
|
2024-03-19 11:09:07 +08:00
|
|
|
|
2024-04-08 13:50:23 +08:00
|
|
|
class SkDateUtil {
|
2024-03-20 18:31:37 +08:00
|
|
|
/// 格式化日期 默认 YYYY-MM-DD
|
|
|
|
static String format(DateTime date, {List<String>? formats}) {
|
2024-04-08 13:50:23 +08:00
|
|
|
return formatDate(date, formats ?? ['yyyy', '-', 'mm', '-', 'dd']);
|
2024-03-20 18:31:37 +08:00
|
|
|
}
|
|
|
|
|
2024-03-19 13:27:42 +08:00
|
|
|
/// 获取几月
|
2024-03-19 11:09:07 +08:00
|
|
|
static String getMonth(DateTime date) {
|
2024-03-19 13:27:42 +08:00
|
|
|
String formattedDate = '${date.month}月';
|
2024-03-19 11:09:07 +08:00
|
|
|
return formattedDate;
|
|
|
|
}
|
|
|
|
|
2024-03-19 13:27:42 +08:00
|
|
|
/// 获取几号
|
2024-03-19 11:09:07 +08:00
|
|
|
static String getDate(DateTime date) {
|
|
|
|
String formattedDate = formatDate(date, ['d']);
|
|
|
|
if (formattedDate.length == 1) {
|
|
|
|
formattedDate = '0$formattedDate';
|
|
|
|
}
|
|
|
|
return formattedDate;
|
|
|
|
}
|
|
|
|
|
2024-03-19 13:27:42 +08:00
|
|
|
/// 获取周几
|
2024-03-19 11:09:07 +08:00
|
|
|
static String getDay(DateTime date) {
|
2024-03-19 13:27:42 +08:00
|
|
|
List<String> daysOfWeekInChinese = ['一', '二', '三', '四', '五', '六', '日'];
|
|
|
|
return '星期${daysOfWeekInChinese[date.weekday - 1]}';
|
2024-03-19 11:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static String addPrefix(String string) {
|
|
|
|
if (string.length == 1) {
|
|
|
|
string = '0$string';
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
2024-04-08 13:50:23 +08:00
|
|
|
|
|
|
|
/// 使用timeago距现在多久
|
|
|
|
static String howLongAgo(DateTime date) {
|
|
|
|
return timeago.format(date, locale: 'zh_cn'); //
|
|
|
|
}
|
2024-03-19 11:09:07 +08:00
|
|
|
}
|