Calculate time from text

// #popclip
// name: TimeSum
// lang: javascript

function calculateTotalTime(text) {
    const timePattern = /\((\d{2}):(\d{2})\)/g;
    let match = timePattern.exec(text);
    let totalMinutes = 0;
    let totalSeconds = 0;
    
    while (match != null) {
        totalMinutes += parseInt(match[1]);
        totalSeconds += parseInt(match[2]);
        match = timePattern.exec(text);
    }

    totalMinutes += Math.floor(totalSeconds / 60);
    totalSeconds = totalSeconds % 60;

    return `${totalMinutes.toString().padStart(2, '0')}:${totalSeconds.toString().padStart(2, '0')}`;
}

popclip.showText(calculateTotalTime(popclip.input.text))

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)

Demo:
CleanShot 2024-01-30 at 10.13.59

2 Likes