skip to Main Content

So im trying to get an array of timestamps from 2 times in flutterflow.
ex. time1 06:00, time2 06:05
the result would be 06:00, 06:01, 06:02, 06:03, 06:04, 06:05.

Im using a custom function so somebody please help

DateTime occupiedTime(DateTime from, DateTime to,)  {
  /// MODIFY CODE ONLY BELOW THIS LINE

 from = DateTime(from.hour, from.minute,);
  to = DateTime(to.hour, to.minute);
  return 
  /// MODIFY CODE ONLY ABOVE THIS LINE
}

i tried this code but no matter what i try to return it gives me error

2

Answers


  1. Based on your input & output, you are trying to get List time (interval based on minute).

    It can be

      List<DateTime> occupiedTime(DateTime from, DateTime to) {
        final differenceInMinutes = to.difference(from).inMinutes;
        return List.generate(differenceInMinutes, (index) {
          return from.add(Duration(minutes: index));
        });
      }
    

    While you like to format, you can use packages/intl or

      String getTimeString(DateTime dateTime) {
        return '${dateTime.hour}:${dateTime.minute}';
      }
    

    Test snippet

      final result = occupiedTime(DateTime.now(), DateTime.now().add(Duration(minutes: 5)));
      final stringTime = result.map((e) => getTimeString(e)).toList();
    
    Login or Signup to reply.
  2. You can define a method like this:

    List<DateTime> occupiedTime(
      DateTime from,
      DateTime to, [
      Duration duration = const Duration(minutes: 1),
    ]) {
      List<DateTime> timestamps = [];
    
      while (from.isBefore(to) || from.isAtSameMomentAs(to)) {
        timestamps.add(from);
        from = from.add(duration);
      }
    
      return timestamps;
    }
    

    Usage:

    void main() {
      DateTime time1 = DateTime(2023, 1, 1, 6, 0);
      DateTime time2 = DateTime(2023, 1, 1, 6, 5);
    
      List<DateTime> result = occupiedTime(time1, time2);
    
      for (DateTime timestamp in result) {
        print(timestamp.toString());
      }
    }
    

    Output:

    2023-01-01 06:00:00.000
    2023-01-01 06:01:00.000
    2023-01-01 06:02:00.000
    2023-01-01 06:03:00.000
    2023-01-01 06:04:00.000
    2023-01-01 06:05:00.000
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search