How to convert flutter TimeOfDay to DateTime?

This is not possible. TimeOfDay holds only the hour and minute. While a DateTime also has day/month/year

If you want to convert it, you will need more information. Such as the current DateTime. And then merge the two into one final datetime.

TimeOfDay t;
final now = new DateTime.now();
return new DateTime(now.year, now.month, now.day, t.hour, t.minute);

You can use DateTime extension

extension DateTimeExtension on DateTime {
  DateTime applied(TimeOfDay time) {
    return DateTime(year, month, day, time.hour, time.minute);
  }
}

then you can use it like this:

final dateTime = yourDate.applied(yourTimeOfDayValue);

and change your sdk version in pubspec.yaml to

environment:
 sdk: ">=2.7.0 <3.0.0"

Tags:

Dart

Flutter