Time Conversion

Sort by

recency

|

859 Discussions

|

  • + 0 comments

    my java solution: ` s= s.trim(); String timeMarker=s.substring(8); String originalTime = s.substring(0, 8); String[] originalTimeList= originalTime.split(":"); String hour = originalTimeList[0]; Integer hourInt = Integer.parseInt(hour); StringBuilder newTime = new StringBuilder();

    if(timeMarker.equals("PM")){
        if(hourInt!=12){
            hourInt+=12;
            hour= String.valueOf(hourInt);
        }
    }
    else{
        if(hourInt==12){
            hour= "00";
        }
    }
    
    return newTime.append(String.valueOf(hour)).append(":")
    .append(originalTimeList[1]).append(":")
    .append(originalTimeList[2]).toString();
    

    `

  • + 0 comments

    how can there be 12AM in a 12hr clock ?!? why the questions are like this?

  • + 0 comments

    My solution in JS

      var splitted = s.split(":")
        var time = splitted[splitted.length -1].substring(2)
        if(time == "PM") {
            var hour = parseInt(splitted[0])
            if(hour != "12") {
                hour = 12 + hour
            }
            return hour + ":" +splitted[1] +":" + splitted[2].substring(0,2)
        } else {
            if(splitted[0] == "12") {
                splitted[0] = "00"
            }
            return splitted[0] + ":" +splitted[1] +":" + splitted[2].substring(0,2)
        }
    
  • + 0 comments

    My Solution to that Problem:

    def timeConversion(s): # Write your code here PM = { "01": "13", "02": "14","03": "15",
    "04": "16", "05": "17", "06":"18", "07": "19", "08":"20", "09": "21", "10":"22", "11": "23", "12":"12"}

    if s[-2] == "P":
        return PM[s[0:2]]+s[2:8] 
    
    if s[-2] == "A" and s[0:2] == "12":
        return "00"+s[2:8]  
    else:
        return s[0:8]  
    
  • + 0 comments

    My answer with Typescript, clearly

    function timeConversion(str: string): string {
        const pad = (n: number) => n.toString().padStart(2, '0')
    
        // 0. read 12h
        let h = Number(str.substring(0, 2))
        let m = Number(str.substring(3, 5))
        let s = Number(str.substring(6, 8))
        let p = str.substring(8, 10)
    
        // 1. convert to 24h
        if (h == 12) h = 0
        if (p == 'PM') h += 12
    
        return `${pad(h)}:${pad(m)}:${pad(s)}`
    }