Flutter/Dart TimeOfDay object returns TimeOfDay(15:00) instead of only the time

2.8k Views Asked by At

i want to print the Time that i initialized manually but if i print it then i only get this here.

I/flutter (14375): TimeOfDay(15:00)

Here is my Code

Future<void> ausgabe(BuildContext context) async {
Map<String, TimeOfDay> zeiten = {"Montag": TimeOfDay(hour: 15, minute: 0)};
print(zeiten["Montag"]);

}

How can i only print 15:00 instead of TimeOfDay(15:00)?

2

There are 2 best solutions below

0
BJW On BEST ANSWER

You can either use:

TimeOfDay time = TimeOfDay(hour: 15, minute: 0);
print(time.format(context));

Or you can do that with DateFormat from the intl flutter package.

print(DateFormat("HH:mm").format(new DateTime(
    2000,
    1,
    1,
    time.hour,
    time.minute)))
1
JideGuru On

Use the format() method

Map<String, TimeOfDay> zeiten = {"Montag": TimeOfDay(hour: 15, minute: 0)};
TimeOfDay time = zeiten["Montag"];
print('${time.hour}:${time.minute.toString().padLeft(2, '0')}');