Time Conversion

  • + 0 comments

    My code of JS/TS

    function timeConversion(s: string): string { // 07:05:45PM
        const isPM = s.endsWith('PM')
        let hh = Number(s.slice(0,2))
        
        if (isPM && hh < 12) { // handle 0-11 hour in PM 
            hh = hh + 12
        } else if (!isPM && hh === 12) { // handle AM hh === 12 cases
            hh = 0
        } 
        const retrunTime = (hh <= 9 ? '0' : '') + hh.toString() + s.slice(2,8) // 19:05:45
        return retrunTime
    }