简体   繁体   English

在 flutter 日期范围选择器中将两个日期之间的所有日期作为列表返回

[英]Return all dates between two dates as a list in flutter date range picker

I got only two dates form flutter date_range_picker but I want a list of date between two selected dates.我从 flutter date_range_picker 只有两个日期,但我想要两个选定日期之间的日期列表。 Thanks for your answers感谢您的回答

Try the following:尝试以下操作:

List<DateTime> getDaysInBeteween(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;
  }

There is a problem with the accepted solution.已接受的解决方案存在问题。 The code will run fine most of the time but it will fail because this doesn't considers Daylight saving time (DST) / Daylight time / Summer Time ( https://en.wikipedia.org/wiki/Daylight_saving_time ).该代码大部分时间都可以正常运行,但会失败,因为它不考虑夏令时 (DST) / 夏令时 / 夏令时( https://en.wikipedia.org/wiki/Daylight_saving_time )。

As a result, for example it will generate the following sequence (notice the gap):结果,例如它将生成以下序列(注意间隙):

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

Here it is an alternative solution that will work better, I think.我认为,这是一种效果更好的替代解决方案。

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;
}

I always do like this:我总是这样:

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)));
}

Always use UTC to avoid issues like DST when working with dates such as addition or subtraction and then convert them back to local time.在处理加法或减法等日期时,始终使用 UTC来避免 DST 等问题,然后将它们转换回本地时间。

Also, adding days manually like day + 1 could cause issues if both the start and end dates are not of the same month.此外,如果开始日期和结束日期不在同一月份,手动添加天数(如day + 1 )可能会导致问题。

So to avoid all that do it like this:-所以为了避免所有这样做: -

Case 1 - Get days in between including start and end Date:-案例 1 - 获取包括开始和结束日期之间的天数:-

List<DateTime> getDaysInBetweenIncludingStartEndDate(
    {required DateTime startDateTime, required DateTime endDateTime}) {

  // Converting dates provided to UTC
  // So that all things like DST don't affect subtraction and addition on dates
  DateTime startDateInUTC =
      DateTime.utc(startDateTime.year, startDateTime.month, startDateTime.day);
  DateTime endDateInUTC =
      DateTime.utc(endDateTime.year, endDateTime.month, endDateTime.day);

  // Created a list to hold all dates
  List<DateTime> daysInLocalFormat = [];
  
  // Starting a loop with the initial value as the Start Date
  // With an increment of 1 day on each loop
  // With condition current value of loop is smaller than or same as end date
  for (DateTime i = startDateInUTC;
      i.isBefore(endDateInUTC) || i.isAtSameMomentAs(endDateInUTC);
      i = i.add(const Duration(days: 1))) {
    
    // Converting back UTC date to Local date before inserting in list
    // You can keep in UTC format depending on your case
    daysInLocalFormat.add(DateTime(i.year, i.month, i.day));
    
  }
  return daysInLocalFormat;
}

Case 2 - Get days in between excluding start and end Date:-案例 2 - 获取不包括开始和结束日期之间的天数:-

List<DateTime> getDaysInBetweenExcludingStartEndDate(
    {required DateTime startDateTime, required DateTime endDateTime}) {

  // Converting dates provided to UTC
  // So that all things like DST don't affect subtraction and addition on dates
  DateTime startDateInUTC =
      DateTime.utc(startDateTime.year, startDateTime.month, startDateTime.day);
  DateTime endDateInUTC =
      DateTime.utc(endDateTime.year, endDateTime.month, endDateTime.day);

  // Created a list to hold all dates
  List<DateTime> daysInLocalFormat = [];
  
  // Starting a loop with the initial value as the next day of the Start Date
  // With an increment of 1 day on each loop
  // With condition current value of loop is smaller than end date
  for (DateTime i = startDateInUTC.add(const Duration(days: 1));
      i.isBefore(endDateInUTC);
      i = i.add(const Duration(days: 1))) {

    // Converting back UTC date to Local date before inserting in list
    // You can keep in UTC format depending on your case
    daysInLocalFormat.add(DateTime(i.year, i.month, i.day));
  }

  return daysInLocalFormat;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM