JAVASCRIPT
VUE
REACT
NODE
ES6
TYPESCRIPT

Js把文字时间变成毫秒

2021. 07. 14    

把文字时间变成毫秒 1小时1分1秒=>3661000

let str = '1小时1分1秒';
function millisecond(str) {
    let [h, m, s] = [0, 0, 0]
    //小时
    if (str.indexOf('小') != -1) {
        h = str.match(/(\S*)小/)[1] * 3600000
    }
    // 分钟
    if (str.indexOf('分') != -1 && str.indexOf('时') != -1) {
        m = str.match(/时(\S*)分/)[1] * 60000
    } else if (str.indexOf('分') != -1 && str.indexOf('时') == -1) {
        m = str.match(/(\S*)分/)[1] * 60000
    }
    // 秒数 
    if (str.indexOf('分') != -1 && str.indexOf('秒') != -1) {
        s = str.match(/分(\S*)秒/)[1] * 1000
    } else if (str.indexOf('时') != -1 && str.indexOf('秒') != -1) {
        s = str.match(/时(\S*)秒/)[1] * 1000
    } else if (str.indexOf('分') == -1 && str.indexOf('秒') != -1 || str.indexOf('时') == -1 && str.indexOf('秒') != -1) {
        s = str.match(/(\S*)秒/)[1] * 1000
    }
    return h + m + s
}
console.log(millisecond(str))