Home > Mobile >  How to create time slot with 2 hours interval in dart/flutter
How to create time slot with 2 hours interval in dart/flutter

Time:01-10

I want time slot of two hour interval output like this List timeSlots = ['8:00 AM - 10:00 AM','10:00 AM','12:00 AM',....] is there any pub packages to get this ?

CodePudding user response:

check below code for it.

void timeSlots({int start = 0,int end = 24, int hourDiff = 2}){
  List<String> slots = [];
  
  int i = 0;
  while(i <= 24){
    
    if(i< 10){
      slots.add('0$i:00 AM');
    }else if(i< 12){
      slots.add('$i:00 AM');
    }else if(i == 12){
      slots.add('$i:00 PM');
    }else if(i == 24){
      //do nothing already added 12:00AM or 00:00AM
    }else{
      slots.add('${i}:00 PM');
    }
    
    i = i   hourDiff;
  }
  
}

CodePudding user response:

Use Flutter Package time: ^2.1.0 and refer More by Package's Documentation.

For two Hour Interval Use

final Duration interval = 2.hours;
  •  Tags:  
  • Related