pretty_duration 1.0.0
pretty_duration: ^1.0.0 copied to clipboard
Formats a [Duration] into a readable string.
pretty_duration #
A small Dart utility to format Duration objects into human-readable strings.
- Simple, zero-dependency, and null-safe.
- Supports short (
1h 30m) and verbose (1 hour 30 minutes) formats. - Skips zero-value units by default for clean output.
Features #
- Format
Durationto short or verbose strings. - Automatically skips zero-value units by default.
- Returns
"0s"/"0 seconds"if the duration is empty. - Fully null-safe and Dart 3 compatible.
Installation #
Add this to your pubspec.yaml:
dependencies:
pretty_duration: ^1.0.0
Then run:
dart pub get
Usage #
import 'package:pretty_duration/pretty_duration.dart';
void main() {
final d = Duration(days: 1, hours: 2, minutes: 5, seconds: 0);
// Short format (default)
print(prettyDuration(d));
// Output: "1d 2h 5m"
// Verbose format
print(prettyDuration(d, verbose: true));
// Output: "1 day 2 hours 5 minutes"
// Keep zero units (optional)
print(prettyDuration(d, skipEmpty: false));
// Output: "1d 2h 5m 0s"
// Empty duration
print(prettyDuration(Duration.zero));
// Output: "0s"
}
API #
String prettyDuration(Duration duration, {bool verbose = false, bool skipEmpty = true}) #
- duration: The
Durationto format. - verbose: If
true, uses full words ("2 hours 5 minutes"). - skipEmpty: If
true(default), removes units with value0.
Returns a human-readable string representing the duration.
Example Output #
| Duration | Default | Verbose | Skip Empty: false |
|---|---|---|---|
Duration(hours: 1, minutes: 30) |
1h 30m |
1 hour 30 minutes |
0d 1h 30m 0s |
Duration(seconds: 45) |
45s |
45 seconds |
0d 0h 0m 45s |
Duration.zero |
0s |
0 seconds |
0d 0h 0m 0s |
Contributing #
Contributions are welcome! Feel free to open issues or submit pull requests.
License #
MIT License © 2025 bpixelq