Skip to main content
The createMs() function creates a new instance of the ms function with custom default options. This is useful when you want to use a specific language or apply consistent formatting options throughout your application.

Import

import { createMs } from 'enhanced-ms';

Signature

function createMs(options?: CreateMsOptions): Ms
options
CreateMsOptions
Configuration options for the ms instance
return
Ms
A new function that formats and parses durations using the specified options as defaults

Examples

Create with language

import { createMs } from 'enhanced-ms';

const ms = createMs({ language: 'en' });
ms(90061000)
// Returns: "1 day 1 hour 1 minute"

const msRu = createMs({ language: 'ru' });
msRu(90061000)
// Returns: "1 день 1 час 1 минута"

const msDe = createMs({ language: 'de' });
msDe(90061000)
// Returns: "1 Tag 1 Stunde 1 Minute"

Create with default format options

import { createMs } from 'enhanced-ms';

const ms = createMs({
  formatOptions: {
    useAbbreviations: true,
    unitLimit: 2
  }
});

ms(90061000)
// Returns: "1d 1h" (uses default options)

ms(90061000, { unitLimit: 1 })
// Returns: "1d" (overrides unitLimit)

Create with preset

import { createMs } from 'enhanced-ms';

const ms = createMs({
  formatOptions: 'short'
});

ms(90061000)
// Returns: "1d 1h"

Create with custom language definition

import { createMs } from 'enhanced-ms';

const customLanguage = {
  decimal: '.',
  units: {
    year: {
      name: 'year',
      abbreviation: 'y',
      matches: ['y', 'yr', 'year', 'years']
    },
    // ... define other units
  }
};

const ms = createMs({ language: customLanguage });

Type definition

interface CreateMsOptions {
  /**
   * The language to use for formatting and parsing.
   * @default 'en'
   */
  language?: keyof typeof languages | LanguageDefinition;

  /**
   * Default formatting options to use.
   */
  formatOptions?: FormatOptions | FormatOptionsPreset;
}

Notes

  • The default export of Enhanced MS is created using createMs({ language: 'en' })
  • Options passed to createMs() are used as defaults and can be overridden on each function call
  • The returned function has the same interface as the main ms() function

See also

Build docs developers (and LLMs) love