mobile_skt/lib/util/date.util.dart

43 lines
1.1 KiB
Dart
Raw Permalink Normal View History

2024-10-16 09:48:17 +08:00
import 'package:date_format/date_format.dart';
import 'package:timeago/timeago.dart' as timeago;
class SkDateUtil {
/// 格式化日期 默认 YYYY-MM-DD
static String format(DateTime date, {List<String>? formats}) {
return formatDate(date, formats ?? ['yyyy', '-', 'mm', '-', 'dd']);
}
/// 获取几月
static String getMonth(DateTime date) {
String formattedDate = '${date.month}';
return formattedDate;
}
/// 获取几号
static String getDate(DateTime date) {
String formattedDate = formatDate(date, ['d']);
if (formattedDate.length == 1) {
formattedDate = '0$formattedDate';
}
return formattedDate;
}
/// 获取周几
static String getDay(DateTime date) {
List<String> daysOfWeekInChinese = ['', '', '', '', '', '', ''];
return '星期${daysOfWeekInChinese[date.weekday - 1]}';
}
static String addPrefix(String string) {
if (string.length == 1) {
string = '0$string';
}
return string;
}
/// 使用timeago距现在多久
static String howLongAgo(DateTime date) {
return timeago.format(date, locale: 'zh_cn'); //
}
}