我有一个不同的DateTime列表,一年中的每个月有7-15天,间隔几天。例如: 01.07,04.07,09.07,14.07,20.07……,04.08,10.08问题:如何检查该日期是否是给定月份的最后一个日期?例如,23.07可能是07月份的最后一个日期。谢谢

我需要一个函数来检查。作为输入,我得到一个由Bloc增强的DateTime,所以我需要一个check函数。
发布于 2022-10-09 17:48:10
只需将一个添加到日期,看看是否在下个月:
void main(List<String> arguments) {
for (final w
in '2022-01-01 2022-01-30 2022-01-31' ' 2022-02-01 2022-02-28 2024-02-28'
.split(' ')) {
// print(w);
final wd = DateTime.parse(w);
final isLastDay = isLastDayOfMonth(wd);
print('$w is last day of month? $isLastDay');
}
}
bool isLastDayOfMonth(DateTime when) {
return DateTime(when.year, when.month, when.day + 1).day == 1;
}
### output:
2022-01-01 is last day of month? false
2022-01-30 is last day of month? false
2022-01-31 is last day of month? true
2022-02-01 is last day of month? false
2022-02-28 is last day of month? true
2024-02-28 is last day of month? false发布于 2022-10-09 17:34:57
我会过滤这个月,对列表进行排序,然后输入第一个条目:
void main() {
List<DateTime> list = [DateTime(2000,06,23), DateTime(2000,06,21),DateTime(2000,06,22)];
list = list.where((date) => (date.month == 6)).toList();
list.sort((a,b) => b.day.compareTo(a.day));
print(list[0]);
}https://stackoverflow.com/questions/74006620
复制相似问题