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.
- Time Conversion
- Discussions
Time Conversion
Time Conversion
+ 0 comments Java :
public static String timeConversion(String s) {
String ampm = s.substring(8, 10); int hrs = Integer.parseInt(s.substring(0, 2)); String str=""; if(ampm.equalsIgnoreCase("PM")) { if(hrs!=12) { hrs+=12; } str = String.valueOf(hrs)+s.substring(2, 8); } else{ if(hrs==12) { str = "00"+s.substring(2, 8); } else if(hrs<10) { str = "0"+String.valueOf(hrs)+s.substring(2, 8); } } return str; }
+ 0 comments This is my solution:
def timeConversion(s): # Write your code here if s.rfind("PM") == 8 and s.split(":")[0] != "12": s = s.strip("PM").replace(s.split(":")[0],str(int(s.split(":")[0])+12)) elif s.rfind("AM") == 8 and s.split(":")[0] == "12": s = s.strip("AM").replace("12","00") else: s = s.strip("AM").strip("PM") return s
+ 0 comments Had some fun with this one JS Let me know how I can imporve!!
function timeConversion(s) { // Write your code here let hrs = s.slice(0,2) let min = s.slice(3,5) let sec = s.slice(6,8) let amPm = s.slice(8,10) if( amPm === 'PM'){ hrs = parseInt(hrs) if(hrs < 12){ hrs = hrs + 12 } } else if (amPm === "AM"){ if (hrs === '12'){ hrs = '00' } } return `${hrs}:${min}:${sec}` }
+ 0 comments This is my solution using JavaScript
function timeConversion(s) { //Variables to hold the individual time pieces let hrs = s.slice(0,2); const mins = s.slice(3,5); const sec = s.slice(6,8); //Conditional to determine the time conversion if (s.includes("AM")) { if (hrs === '12') { hrs = '00'; } return `${hrs}:${mins}:${sec}`; } else { hrs = parseInt(hrs); if (hrs < 12) { hrs = hrs + 12; } return `${hrs}:${mins}:${sec}`; } }
+ 0 comments public static String timeConversion(String s) { // Write your code here String output = ""; String newhour = ""; String[] parts = s.split(":|(?=[AP]M)"); String hour = parts[0]; String minute = parts[1]; String second = parts[2].substring(0, 2); String ampm = parts[2].substring(2);
if(ampm.equals("AM")){ if(hour.equals("12")){ newhour = "00"; } else{ newhour = hour; } output = newhour+":"+minute+":"+second; }else{ if(!hour.equals("12")){ int hournumber = Integer.parseInt(hour); hournumber = hournumber+12; newhour = Integer.toString(hournumber); } else{ newhour=hour; } output = newhour+":"+minute+":"+second; } return output; }
what is wrong with this solution ?
Load more conversations
Sort 662 Discussions, By:
Please Login in order to post a comment