我有一個不同日期時間的串列,其中一年中的每個月都有 7-15 天,間隔為幾天。例如:01.07, 04.07, 09.07, 14.07, 20.07..., 04.08, 10.08 問題:如何檢查日期是否是給定月份的最后一個日期?例如,日期 23.07 可能是月份 07 的最后日期。謝謝
我需要一個函式來檢查。作為輸入,我得到一個由 Bloc 增強的 DateTime,所以我需要一個檢查功能。
uj5u.com熱心網友回復:
只需在日期上加一個,看看它是否在下個月:
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
uj5u.com熱心網友回復:
我會過濾月份,對串列進行排序并獲取第一個條目:
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://www.uj5u.com/qukuanlian/512875.html
標籤:扑镖
