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
Configuration options for the ms instance language
keyof typeof languages | LanguageDefinition
default: "'en'"
The language to use for formatting and parsing. Can be a language code (e.g., 'en', 'ru', 'de') or a custom language definition object.
formatOptions
FormatOptions | FormatOptionsPreset
Default formatting options to use. These options will be applied to all formatting operations unless overridden. See FormatOptions for details.
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"
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