Time Conversion

  • + 0 comments

    That's my solution in javascript

    
    
    function timeConversion(s) {
        const isPM = s.includes("PM");
        const isMidnight = s.split(':')[0] == "12";
        
        console.log(isMidnight);
        
        if(!isPM && isMidnight){
           const midnight = s.split(':');
            midnight[0] = "00"
            return midnight.join(":").replace("AM","");
        
        }else if(!isPM){
            return s.replace("AM","");
        
        }else{
            const hourArr = s.replace("PM","").split(":").map(i => Number(i));
            hourArr[0] += 12;
            hourArr[0] == 24? hourArr[0] = 12: hourArr[0]=hourArr[0];
            return hourArr.map(i => {
                if (i >=0 && i < 10){
                    return String( "0" + i)
                }else{
                    return String(i)
                }
            }).join(":")
             
        }
    
    }