As the title says, I got a string '01:23.290', it looks like a Duration, but not. Now I need to use this to compare with a real Duration, and I don't how to deal with it. Is there any methods?
If we are dealing with smaller durations and needed only minutes and seconds: format(Duration d) => d. toString().
Use a parsing function like this, then use the comparison methods of Duration
:
Duration parseDuration(String s) {
int hours = 0;
int minutes = 0;
int micros;
List<String> parts = s.split(':');
if (parts.length > 2) {
hours = int.parse(parts[parts.length - 3]);
}
if (parts.length > 1) {
minutes = int.parse(parts[parts.length - 2]);
}
micros = (double.parse(parts[parts.length - 1]) * 1000000).round();
return Duration(hours: hours, minutes: minutes, microseconds: micros);
}
Package duration
provides functions parseTime
and tryParseTime
to parse duration strings obtained by Duration().toString()
.
Usage is straight forward:
print(parseTime(Duration(hours: 5, seconds: 10, milliseconds: 567).toString()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With