Skip to main content

MsToString

The MsToString() function converts a number of milliseconds to a human readable time string.

Parameters

ParameterTypeDescription
millisecondsNumberThe number of milliseconds to convert.
separatorStringThe separator between units. Defaults to ' ,'.

Time units:

ParameterDescription
sSecond
mMinute
hHour
dDay
wWeek
moMonth
yYear

Returns

  • String: The time string

Throws

  • Error: If the input is not a number or is invalid

Examples

Using the default seperator:

const utils = require('utils-core.js');
const msTime = 12345;
const convertedTime = utils.time.MsToString(msTime);
//Output: '1m'
const utils = require('utils-core.js');
const msTime = 60000;
const convertedTime = utils.time.MsToString(msTime);
//Output: '1m'

Using custom timeStamp: (April 4th, 2022 at 12:00:00 AM UTC)

const utils = require('utils-core.js');
const msTime = 1649164800000;
const convertedTime = utils.time.MsToString(msTime);
//Output: '2mo, 2w, 3d, 12h, 30m, 23s'

Using custom seperator:

const utils = require('utils-core.js');
const msTime = 1649164800000;
const convertedTime = utils.time.MsToString(msTime, ' : ');
//Output: '2mo : 2w : 3d : 12h : 30m : 23s'
const utils = require('utils-core.js');
const msTime = 1649164800000;
const convertedTime = utils.time.MsToString(msTime, ' and ');
//Output: '2mo and 2w and 3d and 12h and 30m and 23s'

ms less than 1000:

const utils = require('utils-core.js');
const msTime = 500;
const convertedTime = utils.time.MsToString(msTime);
//Output: '0.5s'