How to compare time in flutter?

3,072
  static var startShift = DateTime.now();   
  final endShift = DateTime.now().add(Duration(hours: 8));   
  static var startBreak = startShift.add(Duration(hours: 3));   
  final endBreak = startBreak.add(Duration(hours: 1));

if ((startBreak.isAfter(startShift) && startBreak.isBefore(endShift)) &&
    (endBreak.isAfter(startShift) && endBreak.isBefore(endShift))) {
  //--- Break is within a shift.
} else {
  //--- Invalid break;
}
Share:
3,072
Rutvik Gumasana
Author by

Rutvik Gumasana

flutterrun.com

Updated on December 15, 2022

Comments

  • Rutvik Gumasana
    Rutvik Gumasana 11 months

    here I want to validate time. as per this image, when I choose the Shift Begin time and End Time then whenever I am choosing break time it will always between Shift begin time and Shift end time.

    Here is the Image of output

    enter image description here

    Here is the code for the time picker

                           DateTimeField( 
                              decoration: InputDecoration(
                                contentPadding: const EdgeInsets.symmetric(
                                    vertical: 15.0, horizontal: 10),
                                labelText: "Time",
                                filled: true,
                                fillColor: Colors.white,
                                border: OutlineInputBorder(
                                  borderRadius: new BorderRadius.circular(6),
                                ),
                              ),
                              format: format,
                              onShowPicker: (context, currentValue) async {
                                final time = await showTimePicker(
                                  context: context,
                                  initialTime: TimeOfDay.fromDateTime(
                                    currentValue ?? DateTime.now(),
                                  ),
                                );
                                return DateTimeField.convert(time);
                              },
                            ),
    
  • Rutvik Gumasana
    Rutvik Gumasana almost 4 years
    interesting answer wait I'll try and then let you know
  • Rutvik Gumasana
    Rutvik Gumasana almost 4 years
    Here I am selecting the time from DatetimeFormField so how can I define the time for that. because the user can select anything from time picker and shift is not may exact 8 hours it may also change.
  • Anand Kore
    Anand Kore almost 4 years
    What kind of exception? can you share?
  • Rutvik Gumasana
    Rutvik Gumasana almost 4 years
    it okay it just a static member problem
  • Anand Kore
    Anand Kore almost 4 years
    @RutvikGumasana does this solve your problem?? If yes, please mark it as the correct answer. 😊
  • Rutvik Gumasana
    Rutvik Gumasana almost 4 years
    Yes buddy sure but before please check edit i have suggested.
  • Anand Kore
    Anand Kore almost 4 years
    @RutvikGumasana Approved.