Files
food_hub_app/lib/utils/date_util.dart
2025-07-18 17:06:47 +08:00

21 lines
945 B
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/// 获取指定日期所在月份的第一天(返回"yyyy-MM-dd"格式)
/// [date] 可选参数,默认使用当前日期
String getFirstDayOfMonth([DateTime? date]) {
final currentDate = date ?? DateTime.now();
final firstDay = DateTime(currentDate.year, currentDate.month, 1);
return "${firstDay.year}-${_twoDigits(firstDay.month)}-${_twoDigits(firstDay.day)}";
}
/// 获取指定日期所在月份的最后一天(返回"yyyy-MM-dd"格式)
/// [date] 可选参数,默认使用当前日期
String getLastDayOfMonth([DateTime? date]) {
final currentDate = date ?? DateTime.now();
// 下个月第一天减一天即为当月最后一天
final lastDay = DateTime(currentDate.year, currentDate.month + 1, 0);
return "${lastDay.year}-${_twoDigits(lastDay.month)}-${_twoDigits(lastDay.day)}";
}
/// 辅助函数确保数字为两位数如1→"01"
String _twoDigits(int n) {
return n.toString().padLeft(2, '0');
}