Time Conversion

Sort by

recency

|

879 Discussions

|

  • + 0 comments

    Most of the solutions im seeing are overdoing it or using packages (which imo is lowkey cheating LOL) but here's how i kept it beginner friendly 🫡 This also has a time complexity of O(1) iirc

    def timeConversion(s):
        Hours = int(s[0:2]) #indexing the hours
        MinutesSeconds = s[2:8]
        AMPM = s[8:] #indexing the AM/PM
        
        if AMPM == "AM": #if its in the AM
            if Hours == 12:
                Hours = 0
        else: #when its NOT AM so it has to be PM
            if Hours != 12 : #while we're between the hours of 01pm - 11pm   
                 Hours += 12 # add 12 to HH
    
        return f"{Hours:02}{MinutesSeconds}"
    
    #f"{Hours:02} means that the HH should always have two integers, so if it were to be 7:42:41AM, that translates to 07:42:41 where it would be invalid without the 0
    #MinutesSeconds just reserves the mm:ss since we're not touching that, so we can join it together via formatting 
            
    

    with comments because those are important

  • + 0 comments

    include

    using namespace std;

    /* * Complete the 'timeConversion' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */

    string timeConversion(string s) { string period = s.substr(8, 2); // Extract AM or PM string hour_str = s.substr(0, 2); int hour = stoi(hour_str); string remaining = s.substr(2, 6); // Remaining part after hour (":MM:SS")

    if (period == "AM") {
        if (hour == 12) {
            hour_str = "00";
        }
    } else { // PM case
        if (hour != 12) {
            hour += 12;
            hour_str = to_string(hour);
        }
    }
    
    return hour_str + remaining;
    

    }

    int main() { ofstream fout(getenv("OUTPUT_PATH"));

    string s;
    getline(cin, s);
    
    string result = timeConversion(s);
    
    fout << result << "\n";
    
    fout.close();
    
    return 0;
    

    }

  • + 0 comments

    I used Java and tried to keep it simple and easy to read

    public static String timeConversion(String s) {
           int hoursIn24 = 0;
           int hoursIn12 = 0;
           String result = s;
           
           hoursIn12 = Integer.parseInt(s.substring(0, 2));
           
           if (s.indexOf("AM") > 0){
               if (hoursIn12 == 12){
                    result = "00" + s.substring(2, s.length() - 2);
               }
               else{
                    result = s.substring(0, s.length() - 2);
               }
           }
           else{
             if (hoursIn12 == 12){
                    result = s.substring(0, s.length() - 2);
             }
             else{
                hoursIn24 = 12 + hoursIn12;
                result = hoursIn24 + s.substring(2, s.length() - 2);
             }
           }
            return result;
        }
    
  • + 0 comments

    GO

    1. Easy to understand. Using "time" package
    func timeConversion(s string) string {
        milLayout := "15:04:05"
        amPmLayout := "03:04:05PM"
        
        //handle err if u need
        t, _ := time.Parse(amPmLayout, s)
        
        return t.Format(milLayout)
    }
    
    1. algo
    func timeConversion(s string) string {
        out := []rune(s)
    
        var isPm int
        if s[len(out)-2:] == "PM" {
            isPm = 1
        } else {
            isPm = 0
        }
    
        //handle err if u need
        hours, _ := strconv.Atoi(s[:2])
        if isPm == 1 && hours != 12 {
            hours += 12
        } else if isPm == 0 && hours == 12 {
            hours = 0
        }
    
        out[0] = rune(hours/10 + '0')
        out[1] = rune(hours%10 + '0')
    
        out = out[:len(out)-2]
    
        return string(out)
    }
    
  • + 0 comments

    def timeConversion(s): # Write your code here

    hh, mm , ss = map(int, s[:-2].split(":"))
    if s[-2:] == 'AM' and hh == 12:
        hh = 0
    elif s[-2:] == 'PM' and hh != 12:
        hh+=12
    
    return f"{hh:02}:{mm:02}:{ss:02}"
    

    if name == 'main':

    s = input()
    
    result = timeConversion(s)
    
    print(result)