Registry / productivity / duration-format

duration-format

JSON →
library0.0.2jsnpmunverified

A lightweight JavaScript library for formatting durations (milliseconds) into human-readable strings using template patterns with placeholders for days, hours, minutes, seconds, and milliseconds. Version 0.0.2 is the latest and only release, with no active development. It differs from other duration formatters (e.g., moment.js duration, Intl.RelativeTimeFormat) by offering a simple, pattern-based syntax inspired by Ruby's String#% and does not depend on any external libraries. The library works in both Node.js and browser environments and supports zero-padding via number prefix in placeholders.

npm install duration-format
INSTALL
IMPORT
SIG · DURATION-FORMAT
D
duration-format
productivityjavascriptv0.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import df from 'duration-format'
const df = require('duration-format')
Package only provides a CommonJS default export; ESM import works in bundlers but is not officially supported.
df
const df = require('duration-format')
const { df } = require('duration-format')
The library exports a single function as module.exports.
default
const df = require('duration-format').default
const df = require('duration-format')
Not applicable since there is no .default export; this is not a valid pattern.

Formats a duration (in milliseconds) using templates with optional zero-padding placeholders.

const df = require('duration-format'); const S = 1000; const M = 60 * S; const H = 60 * M; const D = 24 * H; const duration = 2*D + 3*H + 45*M + 6*S + 78; console.log(df(duration, '#{D} Day #{H} : #{M} : #{S} . #{MS}')); // Output: 2 Day 3 : 45 : 6 . 78 console.log(df(duration, '#{4D} Day #{2H} : #{2M} : #{2S} . #{3MS}')); // Output: 0002 Day 03 : 45 : 06 . 078 console.log(df(duration, '#{2H} : #{2M} : #{2S}')); // Output: 51 : 45 : 06
Debug
Known issues
gotchaPlaceholder numbers (e.g., #{4D}) are zero-padded to the specified width, but if the number of digits exceeds the width, the full number is shown; no truncation occurs.
fix
Ensure width is large enough for expected values.
affects: 0.0.2
gotchaThe library does not support locale-specific formatting or timezone handling; all calculations are based on raw milliseconds.
fix
For locale-aware formatting, use Intl.DateTimeFormat or a library like moment.js.
affects: 0.0.2
deprecatedThe package has not been updated since initial release; no active maintenance or support.
fix
Consider using a more maintained library like 'pretty-ms' or 'humanize-duration'.
affects: 0.0.2
gotchaPlaceholder names must be capitalized exactly: D, H, M, S, MS. Lowercase or other variations will not be replaced.
fix
Use only the exact placeholder names: #{D}, #{H}, #{M}, #{S}, #{MS}.
affects: 0.0.2
Errors
Common errors & fixes
Error: Cannot find module 'duration-format'
Package not installed or not present in node_modules.
fix
Run 'npm install duration-format' in your project directory.
TypeError: df is not a function
Incorrect import pattern; likely used destructuring on CommonJS export.
fix
Use 'const df = require("duration-format")' instead of 'const { df } = require("duration-format")'.
Uncaught ReferenceError: require is not defined
Using CommonJS require in a browser context that does not support it.
fix
Use a bundler like webpack, or include the script via a <script> tag after converting to a browser-compatible file.
Upgrade
Version history
0.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
27 hits · last 30 days
node
24
OpenAI (training)
1
Resources
duration-format — npm install duration-format · libregistry