Utilities to make working with 'Duration's easier.
Use Duration.pretty() method or prettyDuration function to format a duration.
main() {
final dur =Duration(
days:5,
hours:23,
minutes:59,
seconds:59,
milliseconds:999,
microseconds:999,
);
// => 5d, 23h, 59m, 59sprint(dur.pretty());
// => 3 secondsprint((aMillisecond *3000).pretty());
// => 2 seconds 250 millisecondsprint((aMillisecond *2250).pretty);
// => 1 day 3 hours 2 minutesprint((aMillisecond *97320000).pretty());
}Use locale parameter to format with desired locale.
main() {
// => 5 días 9 horasfinal dur = aDay *5+ anHour *9;
print(
dur.pretty(
abbreviated:false,
locale:DurationLocale.fromLanguageCode('ru'),
));
}Use abbreviated parameter to use abbreviated units.
main() {
final dur =Duration(
days:5,
hours:23,
minutes:59,
seconds:59,
milliseconds:999,
microseconds:999,
);
// => 5d, 23h, 59m, 59s, 999ms, 999usprint(dur.pretty(abbreviated:true, tersity:DurationTersity.all));
}Use spacer to add a string between amount and unit.
main() {
// => 5 whole days 9 whole hoursprint((aDay *5+ anHour *9).pretty(spacer:' whole '));
}Use delimiter to separate each individual part with a string.
main() {
// => 5 days, 9 hours and 10 minuteprint((aDay *5+ anHour *9+ aMinute *10).pretty(delimiter:', '));
}Use conjugation to add a string before the final unit. Use it in conjunction with
delimiter to add ',' and 'and' to separate individual parts.
main() {
// => 5 days, 9 hours and 10 minutesprint(
(aDay *5+ anHour *9+ aMinute *10).pretty(
delimiter:', ',
conjugation:' and ',
));
}main() {
finalDuration dur =parseDuration('245:09:08.007006');
print(dur);
}main() {
finalDuration dur =parseTime('245:09:08.007006');
print(dur);
}