Time Conversion

Sort by

recency

|

271 Discussions

|

  • + 0 comments

    JavaScript

    function timeConversion(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 together
    
      const arr = s.split(":");
      const firstPart = arr[0];
      const secondPart = arr[1];
      const thirdPart = arr[2];
      const id = thirdPart.slice(-2);
    
      let newFirstPart = "";
      if (Number(firstPart) < 12 && id === "AM") {
        newFirstPart = firstPart;
      } else if (Number(firstPart) < 12 && id === "PM") {
        newFirstPart = formatTwoDigits(Number(firstPart) + 12);
      } else if (Number(firstPart) === 12 && id === "AM") {
        newFirstPart = formatTwoDigits(0);
      } else if (Number(firstPart) === 12 && id === "PM") {
        newFirstPart = firstPart;
      }
    
      return newFirstPart + ":" + secondPart + ":" + thirdPart.slice(0, 2);
    }
    

    function formatTwoDigits(num) { return num < 10 ? "0" + num : num; }

  • + 0 comments

    C# without using DateTime:

    public static string timeConversion(string s)
    {
        bool isPM = s.Substring(8, 2) == "PM";
        string hourFormat = s.Substring(0, 8);
        int hour = int.Parse(hourFormat.Substring(0, 2));
        hourFormat = hourFormat.Remove(0, 2);
    
        if (!isPM)
        {
            if(hour == 12)
            return "00" + hourFormat;
    
            return hour < 9 ? "0" + hour + hourFormat : hour + hourFormat;
        }
    
        hour = hour == 12 ? hour : hour + 12;
        
    
        return hour + hourFormat;
    }
    
  • + 0 comments

    My solution in Java

    public static String timeConversion(String s) {
        // Write your code here
            String hours = s.substring(0, 2);
            String mins = s.substring(3, 5);
            String sec = s.substring(6, 8);
            
            if(s.contains("PM") && hours.equals("12")){
                return("12:" + mins + ":" + sec);
            }
            else if(s.contains("PM")){
                return((Integer.parseInt(hours) + 12)+ ":" + mins + ":" + sec);
            }
            else if (s.contains("AM") && hours.equals("12")){
                return("00:" + mins + ":" + sec);
            }else{
                return(hours+ ":" + mins + ":" + sec);
            }
    
        }
    
  • + 0 comments

    Python

    def timeConversion(s):
        # Write your code here
        time_split = s.split(':')
        hours = time_split[0]
        mins = time_split[1]
        secs = time_split[2][:-2]
        if 'AM' in s:
            if '12' in hours:
                hours = '00'
        else:
            if '12' not in hours:
                hours = str(int(hours) + 12)
        return(hours + ":" + mins + ":" + secs)
    
  • + 0 comments

    Java

    public static String timeConversion(String s) {
        // Extract the hour from the string
        int hour;
        if (s.charAt(2) == ':') {
            hour = Integer.parseInt(s.substring(0, 2)); // If there is a colon in the string, then the hour is extracted from the first two characters
        } else {
            hour = Integer.parseInt(s.substring(0, 1)); // Otherwise, the hour is extracted from the first character
        }
    
        // Check for AM/PM and convert the hour
        String result;
        int newHour;
        if (s.charAt(8) == 'P' || s.charAt(7) == 'P') { // If the string contains 'P', then it is PM
            newHour =  hour == 12 ? 12 : hour + 12; // If the hour is 12, then leave it as is, otherwise add 12
        } else {
            newHour =  hour == 12 ? 0 : hour; // If it's not PM and the hour is 12, then set it to 0, otherwise leave it as is
        }
    
        result = "%02d:%s".formatted(
                newHour,
                s.charAt(2) == ':' ? s.substring(3, s.length() - 2) : s.substring(2, s.length() - 2)
        ); // Format the result by adding leading zeros to the hour and returning the remaining part of the string after extracting the hour
    
        return result;
    }