Time-Duration-Stringify
This package lets you transform a time duration into a nicely-formatted string. Provide an integer duration in milliseconds, and get a time duration of either the format "5:03", or "5 minutes, and 3 seconds".
Installation
NPM: npm install --save time-duration-stringify
YarnPkg: yarn add time-duration-stringify
Examples
let Duration = require("time-duration-stringify");
let durationMillis = 3725000;
let result1 = Duration.toStringShort(durationMillis);
let result2 = Duration.toStringLong(durationMillis);
let durationMillis = 301068;
let result1 = Duration.toStringShort(durationMillis);
let result2 = Duration.toStringLong(durationMillis);
let durationMillis = 125000
let result1 = Duration.toStringLong(durationMillis);
let result2 = Duration.toStringLong(durationMillis, true);
API Reference
The largest unit of time supported is days. Thus the recognized separations are days, hours, minutes, seconds, and milliseconds.
Duration.toStringShort(durationMillis);
durationMillis
: a positive integer number representing a duration in milliseconds (throws error if invalid)
Return value: a string of format "4:02:35.749"
Corner cases:
Duration.toStringShort(0) -> "0:00"
Duration.toStringShort(130) -> "0:00.130"
Duration.toStringShort(2000) -> "0:02"
Duration.toStringLong(durationMillis, omitOxfordComma);
durationMillis
: a positive integer number representing a duration in milliseconds (throws error if invalid)
omitOxfordComma
: an optional boolean flag to omit the Oxford comma (eg "4 hours, 2 minutes, 35 seconds and 749 milliseconds)
Return value: a string of format "4 hours, 2 minutes, 35 seconds, and 749 milliseconds"
Corner cases:
Duration.toStringShort(0) -> "0 seconds"
Duration.toStringShort(3600000) -> "1 day"