21 lines
945 B
Dart
21 lines
945 B
Dart
/// 获取指定日期所在月份的第一天(返回"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');
|
||
} |