4.0.0 β€’ Published 3 months ago

enhanced-ms v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

πŸ€” About

enhanced-ms is a flexible library for formatting milliseconds into human-readable durations and vice versa. It is an enhanced version of the popular ms with support for multiple inputs, localization, and more options.

πŸ“ Table of Contents

πŸ“¦ Installation

Install using your preferred package manager:

npm install enhanced-ms
pnpm add enhanced-ms
yarn add enhanced-ms

🧭 Comparison

As mentioned above, enhanced-ms is an enhanced version of the popular ms, so how does it compare?

Featureenhanced-msmspretty-msitty-time
Convert Milliseconds to Durationβœ…βœ…βœ…βœ…
Convert Milliseconds to Multiple Duration Unitsβœ…βŒβŒβŒ
Convert Duration to Millisecondsβœ…βœ…βŒβœ…
Convert Multiple Duration Units to Millisecondsβœ…βŒβŒβŒ
Localization Supportβœ…βŒβŒβŒ

🌐 Languages

The currently supported languages include:

LanguageKey
English (default)en
Germande
Russianru
Māorimi

You can help by adding support for more languages.

Make a pull request here.

πŸš€ Usage

Language and Default Options

The createMs function allows you to create a new instance of ms with a custom language and custom default options. This is useful if you want to use a different language or prefer different default options for parsing and formatting.

function createMs(options?: CreateMsOptions): typeof ms;
OptionTypeDescriptionDefault
languageLocale | LanguageDefinitionThe language to use for parsing and formatting, if your preferred isn't supported, you can directly pass a language definition.en
formatOptionsFormatOptions | FormatOptionsPresetThe options to use for formatting.see below

Formatting Milliseconds to Duration

The ms function allows you to format a number of milliseconds to a duration string. Passing a number of milliseconds will return a duration string, if the number is invalid, it will return null. The milliseconds overloads also allows you to pass a FormatOptions object or a FormatOptionsPreset to customise the formatting.

function ms(milliseconds: number): string | null;
function ms(milliseconds: number, options: FormatOptions): string | null;
function ms(milliseconds: number, preset: FormatOptionsPreset): string | null;
OptionTypeDescriptionDefault
extendsFormatOptionsPresetExtends the preset with the given options.none
hideUnitNamesbooleanHide unit names from the output.false
useAbbreviationsbooleanUse abbreviations for unit names.false
includeZerobooleanInclude units with the value 0 in the output.false
includeMsbooleanInclude milliseconds in the output.false
includeSubMsbooleanInclude sub-millisecond units in the output.false
includedUnitsParseUnit[]Which units should be included in the output.['year', 'day', 'hour', 'minute', 'second']
unitLimitnumberThe maximum number of units to include in the output.-1
unitSeparatorstringThe separator to use between units.
minimumDigitsnumberThe minimum number of digits for a unit, aka will pad with zeroes.0
PresetExample
short1m 30s
fullPrecision10 seconds 100 milliseconds 100 microseconds 100 nanoseconds
colonNotation00:01:30

Parsing Duration to Milliseconds

The ms function also allows you to parse a duration string (1 day, 3 weeks 4 days, etc). Passing a duration string will return a number of milliseconds, if no valid duration units are found, it will return 0.

function ms(duration: string): number;
4.0.0

4 months ago

3.1.0

5 months ago

3.0.0

1 year ago

2.4.0-canary.1

1 year ago

2.3.0

2 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.0.2

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.2

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago