We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functiontimeConversion(s){// spit the time string into 3 parts// extract the letters from the 3rd part// in the 1st part, add 12 if its PM, and subtract 12 if its 12 pm// concatenate everything togetherconstarr=s.split(":");constfirstPart=arr[0];constsecondPart=arr[1];constthirdPart=arr[2];constid=thirdPart.slice(-2);letnewFirstPart="";if(Number(firstPart)<12&&id==="AM"){newFirstPart=firstPart;}elseif(Number(firstPart)<12&&id==="PM"){newFirstPart=formatTwoDigits(Number(firstPart)+12);}elseif(Number(firstPart)===12&&id==="AM"){newFirstPart=formatTwoDigits(0);}elseif(Number(firstPart)===12&&id==="PM"){newFirstPart=firstPart;}returnnewFirstPart+":"+secondPart+":"+thirdPart.slice(0,2);}
function formatTwoDigits(num) {
return num < 10 ? "0" + num : num;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Time Conversion
You are viewing a single comment's thread. Return to all comments →
JavaScript
function formatTwoDigits(num) { return num < 10 ? "0" + num : num; }