skip to Main Content

I have a list that returns from my server like this:

final userTimezone = currentUser?.timezone; // "America/New_York"

final list = [
  {id: 1, name: 'thing 1', startTime: 2023-07-14 07:30:00.000Z},
  {id: 2, name: 'thing 2', startTime: 2023-07-20 08:00:00.000Z},
  {id: 3, name: 'thing 3', startTime: 2023-07-25 20:15:00.000Z},
  {id: 4, name: 'thing 4', startTime: 2023-07-26 13:00:00.000Z},
];

Which all returns in UTC timezone.

What I want to do is to filter this list so that are only items in between the hours of 8AM (08:00) – 7PM (19:00), relevant to the user’s timezone.

This is what I’ve tried, but I don’t think it’s necessarily correct:

list = list.where((appointment) {
        final start = appointment.startAt.toLocal();
        final end = appointment.endAt.toLocal();
        final startHour = start.hour;
        final endHour = end.hour;
        final startMinute = start.minute;
        final endMinute = end.minute;
        final startSecond = start.second;
        final endSecond = end.second;

        final isStartValid = startHour >= 8 && startHour <= 19 && startMinute == 0 && startSecond == 0;
        final isEndValid = endHour >= 8 && endHour <= 19 && endMinute == 0 && endSecond == 0;

        return isStartValid && isEndValid;
      }).toList();

I have a few items that seem to fall within the acceptable range (not listed in the example), but it doesn’t appear in the list.

I am a bit new to dart so I’m not entirely sure what is available to me or not. Thank you for reading.

2

Answers


  1. Right now, your times will only be valid if the minutes and seconds are 0, so an appointment that falls at 3:30 PM, for example, will get filtered out.

    You’re on the right track, but the issue is that you’re concerning yourself with minutes and seconds when they ultimately don’t matter. Think about it – if the hour itself falls between 8:00 AM and 7:00 PM, what could the minutes and seconds possibly be to make the overall timestamp not fall in that range?

    final filtered = list.where((appt) => {
      final start = appt.startAt.toLocal();
      final end = appt.endAt.toLocal();
    
      return (start.hour >= 8 && start.hour < 19) 
          && (end.hour >= 8 && end.hour < 19);
    }).toList();
    
    Login or Signup to reply.
  2. I wrote this using forEach, (timezone doesn’t make difference you can use the code with it):

    List<DateTime> dates = [
    DateTime.parse('2023-07-14 07:30:00.000'),
    DateTime.parse('2023-07-14 07:30:00.000'),
    DateTime.parse('2023-07-20 08:00:00.000'),
    DateTime.parse('2023-07-26 13:00:00.000'),
    DateTime.parse('2023-07-20 14:00:00.000'),
    DateTime.parse('2023-07-20 19:00:00.000'),
    DateTime.parse('2023-07-25 20:15:00.000'),
    DateTime.parse('2023-07-25 21:15:00.000'),
    DateTime.parse('2023-07-25 22:15:00.000'),
      ];
    
    List<DateTime> new_dates = [];
     
    dates.forEach((e) {
       if (e.hour >= 8 && e.hour <= 19) {
           new_dates.add(e);
       }
      });
    print(new_dates);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search