toTimeOfDay method

TimeOfDay? toTimeOfDay()

will parse string to time of days example 19:16 or 07:18 pm

Implementation

TimeOfDay? toTimeOfDay() {
  if (this == null) return null;
  var str = this!.toLowerCase();
  var splits = str.replaceAll(RegExp('[^0-9:]'), '').split(':');
  if (splits.length >= 2) {
    var hour = int.tryParse(splits[0]);
    var minute = int.tryParse(splits[1]);
    if (hour != null && minute != null) {
      if (str.contains(RegExp('[^0-9:]'))) {
        if (str.toLowerCase().contains('pm')) hour += 12;
      }
      if (hour == 24) hour = 0;
      return TimeOfDay(hour: hour, minute: minute);
    }
    return null;
  } else {
    return null;
  }
}