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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Time Conversion
  2. Discussions

Time Conversion

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 662 Discussions, By:

recency

Please Login in order to post a comment

  • gawandeg401
    22 hours ago+ 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|
    Permalink
  • anthonyakil
    2 days ago+ 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|
    Permalink
  • mattrowley10
    3 days ago+ 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|
    Permalink
  • nesquik987
    4 days ago+ 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|
    Permalink
  • sharma_ankur2113
    6 days ago+ 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 ?

    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy