Good day, I have the variable time = 03:50 , I need to convert this time (three minutes fifty seconds) to milliseconds. Then to use in the function window.setTimeout (); Who can share experience, how to do it better? Thank.
- oneYou can do it in the forehead: multiply the minutes by 60,000, seconds - by 1000 and add. - Vladimir Martyanov
|
3 answers
function toms(mmss) { if(typeof mmss !== 'string') { if(mmss.toString) mmss = mmss.toString(); else throw("Invalid input"); } var parts = mmss.split(':') ,n = parts.length ,ms = 0 ,i ; for(i = 0; i < parts.length; i++) { part = parseInt( parts[n - 1 - i]); if(i === 0) { ms += part * 1000; } else if(i === 1) { ms += part * 6e4; } else if(i === 2) { ms += part * 36e5; } } return ms; } toms('01:12') // 72000 toms('01:02:03') // 3723000 toms(55) // 55000 - This universal masterpiece, I think, I will keep) Many thanks. - Demian Shumilov
|
let time = "03:50"; let time_parts = time.split(":"); let millisecond = time_parts[0] * (60000 * 60)) + (time_parts[1] * 60000); |
var split = time.split(":"); var minutes = split[0] * 60000; var seconds = split[1] * 1000; var result = minutes + seconds; This option is also working. Thank you all for the answers.
|