Git Product home page Git Product logo

duration's Introduction

duration pub package

Utilities to make working with 'Duration's easier.

NOTE: Use prettyDuration, prettySeconds, prettyMilliseconds instead of printDuration, printSeconds, printMilliseconds if you only want to format/convert and don't want to print to console!

Format duration

Use printDuration to print a human readable durations. By default, printDuration will print the duration down to the second. It uses english locale by default.

main() {
  final dur = Duration(
    days: 5,
    hours: 23,
    minutes: 59,
    seconds: 59,
    milliseconds: 999,
    microseconds: 999,
  );

  // => 5d, 23h, 59m, 59s
  printDuration(dur);

  // => 3 seconds
  printDuration(aMillisecond * 3000);

  // => 2 seconds 250 milliseconds
  printDuration(aMillisecond * 2250);

  // => 1 day 3 hours 2 minutes
  printDuration(aMillisecond * 97320000);
}

With desired locale

Use locale parameter to format with desired locale.

main() {
  // => 5 días 9 horas
  printDuration(
    aDay * 5 + anHour * 9,
    abbreviated: false,
    locale: DurationLocale.fromLanguageCode('ru'),
  );
}

Abbreviate units

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, 999us
  printDuration(dur, abbreviated: true, tersity: DurationTersity.all);
}

Spacer

Use spacer to add a string between amount and unit.

main() {
  // => 5 whole days 9 whole hours
  printDuration(aDay * 5 + anHour * 9, spacer: ' whole ');
}

Delimiter

Use delimiter to separate each individual part with a string.

main() {
  // => 5 days, 9 hours and 10 minute
  printDuration(aDay * 5 + anHour * 9 + aMinute * 10, delimiter: ', ');
}

Conjugation

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 minutes
  printDuration(
    aDay * 5 + anHour * 9 + aMinute * 10,
    delimiter: ', ',
    conjugation: ' and ',
  );
}

Parse duration

Parse duration

main() {
  final Duration dur = parseDuration('245:09:08.007006');
  print(dur);
}

Parse time

main() {
  final Duration dur = parseTime('245:09:08.007006');
  print(dur);
}

duration's People

Contributors

tejainece avatar kyle-seongwoo-jun avatar shemhazai avatar fatimahossny avatar eligt avatar ivanchaukn avatar lukepighetti avatar simonit avatar stefanop1 avatar tlindi avatar ovchars2 avatar agordeev avatar cretezy avatar charzone95 avatar cooloneofficial avatar cirediew avatar johnpryan avatar reeichert avatar micimize avatar comatory avatar renefloor avatar stark8912 avatar sdabet-orpheo avatar tgarm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.