pretty_duration 1.0.2 copy "pretty_duration: ^1.0.2" to clipboard
pretty_duration: ^1.0.2 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.

example/pretty_duration_example.dart

import 'package:pretty_duration/pretty_duration.dart';

void main() {
  final d = Duration(days: 1, hours: 2, minutes: 5, seconds: 0);

  // Default behavior (skipEmpty: true) with all units
  print(prettyDuration(d));
  // Output: "1d 2h 5m"

  // Keep zero-value units (skipEmpty: false)
  print(prettyDuration(d, skipEmpty: false));
  // Output: "1d 2h 5m 0s"

  // Verbose output, skipEmpty: true
  print(prettyDuration(d, verbose: true));
  // Output: "1 day 2 hours 5 minutes"

  // Verbose output, skipEmpty: false
  print(prettyDuration(d, verbose: true, skipEmpty: false));
  // Output: "1 day 2 hours 5 minutes 0 seconds"

  // Only show hours and minutes
  print(prettyDuration(d, units: [DurationUnit.hours, DurationUnit.minutes]));
  // Output: "2h 5m"

  // Only show hours and minutes with cascade
  print(prettyDuration(d,
      units: [DurationUnit.hours, DurationUnit.minutes], cascade: true));
  // Output: "26h 5m"

  // Only show hours and seconds with cascade
  print(prettyDuration(d,
      units: [DurationUnit.hours, DurationUnit.seconds], cascade: true));
  // Output: "26h 300s"

  // Custom empty text
  print(prettyDuration(Duration.zero, emptyText: "none"));
  // Output: "none"

  // Verbose, only seconds, zero duration
  print(prettyDuration(Duration.zero,
      verbose: true, units: [DurationUnit.seconds]));
  // Output: "0 seconds"
}
1
likes
160
points
58
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