I have a string in the timepicker format "mm:ss", I would like to convert this to milliseconds so I can include it in a setTimeOut. Here is an example of what I intend to do.
let minSeconds="03:30";
let milisenconds= convertMinSeconds(minSeconds)
setTimeout((){
console.log("finish");
},milisenconds)
I don't know if there is an effective way to do this using javascript.
How can I do it? thank you very much
You can try something like this
let minSeconds="03:30";
let milisenconds= convertMinSeconds(minSeconds)
function convertMinSeconds(minSeconds) {
let splitted = minSeconds.split(':')
let min = Number(splitted[0])
let second = Number(splitted[1])
let allInSecond = min*60 + second
let inMilisecond = allInSecond * 1000
return inMilisecond
}
setTimeout(() => {
console.log("finish");
},milisenconds)
this function will solve the problem
const ms = (time)=>{
const [min , sec] = time.split(':')
return ms = ((Number(min) * 60) + Number(sec)) * 1000
}
node
let minSeconds="03:30";
undefined
let miliseconds=new Date("1970-01-01T00:" + minSeconds + "Z").getTime()
undefined
console.log(miliseconds)
210000
undefined
node --version
v10.19.0