pretty_duration 1.0.1 copy "pretty_duration: ^1.0.1" to clipboard
pretty_duration: ^1.0.1 copied to clipboard

Formats a Duration into a readable string with short or verbose output, automatically skipping zero-value units for clean and concise display.

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 Duration to 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 Duration to format.
  • verbose: If true, uses full words ("2 hours 5 minutes").
  • skipEmpty: If true (default), removes units with value 0.

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

1
likes
160
points
56
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Formats a Duration into a readable string with short or verbose output, automatically skipping zero-value units for clean and concise display.

Repository (GitHub)

License

MIT (license)

More

Packages that depend on pretty_duration