Viết hàm nhận chuỗi 'HH:MM:SS', trả tổng giây.
Ví dụ:
countSeconds('12:00:00') //43200 countSeconds('00:30:10') //1810Split chuỗi, parseInt từng phần, tính: giờ*3600 + phút*60 + giây.
Ví dụ
const timeString = '23:54:43'; const other = '12:30:00'; const withoutSeconds = '10:30'; const countSeconds = (str) => { const [hh = '0', mm = '0', ss = '0'] = (str || '0:0:0').split(':'); const hour = parseInt(hh, 10) || 0; const minute = parseInt(mm, 10) || 0; const second = parseInt(ss, 10) || 0; return (hour*3600) + (minute*60) + (second); }; console.log(countSeconds(timeString)); console.log(countSeconds(other)); console.log(countSeconds(withoutSeconds));Kết quả
Đầu ra console:
86083 45000 37800