Time Conversion

Sort by

recency

|

287 Discussions

|

  • + 0 comments
    public static String timeConversion(String s) {
    // Write your code here
    String [] strArray = s.split(":");
    String hour = strArray[0];
    String answer = "";
    Integer hourPM = Integer.parseInt(hour);
    if (s.contains("AM")){
        if(hour.equalsIgnoreCase("12")){
            hour = "00";
        }  
        answer = hour +":"+ strArray[1] +":"+ strArray[2].substring(0,2);
    }else if(s.contains("PM")){
        if (hourPM < 12){
            hourPM = hourPM + 12;
        }
        answer = hourPM +":"+ strArray[1] +":"+ strArray[2].substring(0,2);
    }
    return answer;
    }
    
  • + 0 comments

    Java

    public static String timeConversion(String s) {
            
            // Raw
            int hours   = Integer.parseInt(s.substring(0, 2));
            int minutes = Integer.parseInt(s.substring(3, 5));
            int seconds = Integer.parseInt(s.substring(6, 8));
            
            int transformedHours = hours;
            if (s.contains("PM") && hours != 12) transformedHours = hours + 12;
            else if (s.contains("AM") && hours == 12) transformedHours = 0;
            else transformedHours = hours;
            
            String formattedHours = transformedHours < 10 ? "0" + transformedHours : String.valueOf(transformedHours);
            String formattedMinutes = minutes < 10 ? "0" + minutes : String.valueOf(minutes);
            String formattedSeconds = seconds < 10 ? "0" + seconds : String.valueOf(seconds); 
            
            return String.format(
                "%s:%s:%s",
                formattedHours,
                formattedMinutes,
                formattedSeconds);
        }
    
  • + 0 comments

    C#

    char timeCase = s[8];
            string hourString = string.Concat(s[0],s[1]);
            int hour = int.Parse(hourString);
            
            if(timeCase == 'A')
            {
                if(hour == 12)
                    hourString = "00";
            } else
            {
                if(hour != 12)
                    hourString = (hour+12).ToString();
            }
            return string.Concat(hourString,s[2],s[3],s[4],s[5],s[6],s[7]);
    
  • + 0 comments

    !/bin/python3

    def timeConversion(z): # Write your code here h=0 s=z[:-2] a=s.split(":") if("PM" in z): b=int(a[0]) if(b>=1 and b!=12): h=b+12 print(h,end="") else: print(a[0],end="") if("AM" in z): c=int(a[0]) if(c==12): h=0 print(0,end="") print(h,end="") else: print(a[0],end="") print(":"+a[1],end="") print(":"+a[2])

    z = input() timeConversion(z)

  • + 0 comments

    **Code in Python ** def timeConversion(s): h=s[:2] se=s[-2:] rest=s[2:-2] ho=int(h) if se=='AM': if ho==12: ho=0 else: if ho != 12: ho += 12 return f"{ho:02}{rest}"