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;
}
Comments
-
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
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); }, ),
-
alter123 almost 4 years
-
Trần Đức Tâm almost 4 years@JayVasant It's not Flutter's ways.
-
-
Rutvik Gumasana almost 4 yearsinteresting answer wait I'll try and then let you know
-
Rutvik Gumasana almost 4 yearsHere 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 almost 4 yearsWhat kind of exception? can you share?
-
Rutvik Gumasana almost 4 yearsit okay it just a static member problem
-
Anand Kore almost 4 years@RutvikGumasana does this solve your problem?? If yes, please mark it as the correct answer. 😊
-
Rutvik Gumasana almost 4 yearsYes buddy sure but before please check edit i have suggested.
-
Anand Kore almost 4 years@RutvikGumasana Approved.