A moment.js plugin for formatting durations with comprehensive template-based formatting and localization support.
npx @tessl/cli install tessl/npm-moment-duration-format@2.3.0Moment Duration Format is a comprehensive plugin for Moment.js that adds powerful formatting capabilities to Moment Duration objects. It provides a template-based formatting system with extensive customization options including precision control, intelligent trimming, localization support, and flexible output formatting.
npm install moment-duration-formatCommonJS (Node.js):
var moment = require("moment");
var momentDurationFormatSetup = require("moment-duration-format");AMD:
define(['moment'], factory);Browser (global):
<script src="path/to/moment.js"></script>
<script src="path/to/moment-duration-format.js"></script>Manual setup for other moment packages:
var moment = require("moment-timezone");
var momentDurationFormatSetup = require("moment-duration-format");
momentDurationFormatSetup(moment);var moment = require("moment");
require("moment-duration-format");
// Basic formatting with default template
moment.duration(123, "minutes").format();
// "2:03:00"
// Custom template formatting
moment.duration(3661, "seconds").format("h [hours], m [minutes], s [seconds]");
// "1 hour, 1 minute, 1 second"
// Formatting with precision
moment.duration(3780, "seconds").format("h [hours]", 1);
// "1.1 hours"
// Multiple duration formatting
moment.duration.format([
moment.duration(1, "hour"),
moment.duration(30, "minutes")
], "h:mm");
// ["1:00", "0:30"]Moment Duration Format extends the Moment.js Duration prototype with formatting capabilities:
duration.fn.format) and multi-duration (duration.format) formattingFormat individual moment duration objects with extensive customization options including templates, precision, and comprehensive settings.
/**
* Format a duration using template, precision, and settings
* @param template - Format template string or function (optional)
* @param precision - Number of decimal places or integer truncation (optional)
* @param settings - Configuration object (optional)
* @returns Formatted duration string
*/
moment.duration.fn.format([template] [, precision] [, settings]): stringFormat arrays of duration objects with coordinated output, ensuring consistent formatting across all durations in the set.
/**
* Format multiple durations with coordinated output
* @param durationsArray - Array of moment duration objects
* @param template - Format template string or function (optional)
* @param precision - Number of decimal places or integer truncation (optional)
* @param settings - Configuration object (optional)
* @returns Array of formatted duration strings
*/
moment.duration.format(durationsArray, [template] [, precision] [, settings]): string[]Powerful template engine with moment tokens, escape sequences, auto-localization markers, and dynamic template functions.
// Template tokens: Y/y (years), M (months), W/w (weeks), D/d (days),
// H/h (hours), m (minutes), s (seconds), S (milliseconds)
// Escape sequences: [text]
// Auto-localization: _ (short), __ (standard), _HMS_, _HM_, _MS_Comprehensive settings system controlling trimming, precision, localization, value ranges, and output formatting.
interface FormatSettings {
template?: string | function;
precision?: number;
trim?: string | boolean | null;
largest?: number | null;
useSignificantDigits?: boolean;
// ... 20+ additional configuration options
}Complete internationalization support with auto-pluralization, locale-specific number formatting, and extensible locale definitions.
// Locale extensions for duration labels and pluralization
moment.updateLocale('en', {
durationLabelsStandard: { /* ... */ },
durationLabelsShort: { /* ... */ },
durationTimeTemplates: { /* ... */ },
durationPluralKey: function(token, integerValue, decimalValue) { /* ... */ }
});/**
* Setup function to initialize the plugin on a moment instance
* @param moment - The moment instance to extend
*/
function momentDurationFormatSetup(moment: any): void;The plugin automatically attempts to install on the global moment instance if available, but the setup function can be used to manually initialize on specific moment instances (useful with moment-timezone or other moment variants).
Invalid durations are treated as having a value of 0 for formatting purposes:
var invalidDuration = moment.duration(NaN, "second");
invalidDuration.isValid(); // false
invalidDuration.format(); // "0 seconds"The plugin performs feature tests on initialization to determine the best number formatting method available:
The plugin automatically selects the most capable formatter and handles cross-browser compatibility transparently. Use useToLocaleString: false in settings to force the internal formatter.