Skip to main content

StringToMs

The StringToMs() function converts a time string to milliseconds.

Parameters

ParameterDescription
timeStringThe time string to convert to a Date

Time units:

ParameterDescription
sSecond
mMinute
hHour
dDay
wWeek
MMonth
YYear

Returns

  • Number: The time in milliseconds

Throws

  • Error: If the time string is invalid

Examples

Convert a time string to milliseconds:

const utils = require('utils-core.js');
const timeString = '1m';
const timeInMs = utils.time.StringToMs(timeString);
console.log(timeInMs)
// output: 60000
const utils = require('utils-core.js');
const timeString = '2M';
const timeInMs = utils.time.StringToMs(timeString);
console.log(timeInMs)
// output: 5184000000

Use the time in milliseconds for a setTimeout:

const utils = require('utils-core.js');
const timeString = '5s';
const timeInMs = utils.time,StringToMs(timeString);
setTimeout(() => {
console.log('5 seconds have passed');
}, timeInMs);