Return 两个日期之间的所有日期作为 flutter 日期范围选择器中的列表
Return all dates between two dates as a list in flutter date range picker
我只有两个日期形式 flutter date_range_picker 但我想要两个选定日期之间的日期列表。感谢您的回答
尝试以下操作:
List<DateTime> getDaysInBetween(DateTime startDate, DateTime endDate) {
List<DateTime> days = [];
for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
days.add(startDate.add(Duration(days: i)));
}
return days;
}
我总是这样做:
final today = DateTime.now();
final monthAgo = DateTime(today.year, today.month - 1, today.day);
final List<DateTime> days = [];
for (int i = 0; i <= today.difference(monthAgo).inDays; i++) {
days.add(monthAgo.add(Duration(days: i)));
}
接受的解决方案有问题。该代码在大多数情况下会 运行 正常,但它会失败,因为它没有考虑 夏令时 (DST) / 夏令时 / 夏令时 (https://en.wikipedia.org/wiki/Daylight_saving_time).
因此,例如它将生成以下序列(注意间隙):
2020-10-24 00:00:00.000
2020-10-25 00:00:00.000
2020-10-25 23:00:00.000 # Mind the Gap :)
2020-10-26 23:00:00.000
我认为这是一个更好的替代解决方案。
List<DateTime> getDaysInBeteween(DateTime startDate, DateTime endDate) {
List<DateTime> days = [];
for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
days.add(
DateTime(
startDate.year,
startDate.month,
// In Dart you can set more than. 30 days, DateTime will do the trick
startDate.day + i)
);
}
return days;
}
我只有两个日期形式 flutter date_range_picker 但我想要两个选定日期之间的日期列表。感谢您的回答
尝试以下操作:
List<DateTime> getDaysInBetween(DateTime startDate, DateTime endDate) {
List<DateTime> days = [];
for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
days.add(startDate.add(Duration(days: i)));
}
return days;
}
我总是这样做:
final today = DateTime.now();
final monthAgo = DateTime(today.year, today.month - 1, today.day);
final List<DateTime> days = [];
for (int i = 0; i <= today.difference(monthAgo).inDays; i++) {
days.add(monthAgo.add(Duration(days: i)));
}
接受的解决方案有问题。该代码在大多数情况下会 运行 正常,但它会失败,因为它没有考虑 夏令时 (DST) / 夏令时 / 夏令时 (https://en.wikipedia.org/wiki/Daylight_saving_time).
因此,例如它将生成以下序列(注意间隙):
2020-10-24 00:00:00.000
2020-10-25 00:00:00.000
2020-10-25 23:00:00.000 # Mind the Gap :)
2020-10-26 23:00:00.000
我认为这是一个更好的替代解决方案。
List<DateTime> getDaysInBeteween(DateTime startDate, DateTime endDate) {
List<DateTime> days = [];
for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
days.add(
DateTime(
startDate.year,
startDate.month,
// In Dart you can set more than. 30 days, DateTime will do the trick
startDate.day + i)
);
}
return days;
}