I have this data.
"2mn 56s", "30s 83ms", "2h 10mn"
How to convert these into time format:
hh:mn:ss - "00:02:56", "00:00:30", "02:10:00"
In JavaScript, you can use String#replace method.
var str = '"2mn 56s", "30s 83ms", "2h 10mn"';
console.log(
str.replace(/"(?:(\d)+h\s*)?(?:(\d+)mn\s*)?(?:(\d+)s\s*)?[^"]*"?/g, function(_, m1, m2, m3) {
return [m1, m2, m3].map(v => ('00' +( v || '')).slice(-2)).join(':')
})
)
Regex explanation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With