Time Conversion

Sort by

recency

|

342 Discussions

|

  • + 0 comments

    Kotlin Solution

    fun timeConversion(s: String): String {
        val splitString = s.split(":").toMutableList()
        val obtainFormat = splitString.last().takeLast(2)
         val hour = splitString.first().toInt()
        if(obtainFormat == "AM"){
            splitString[0] = String.format("%02d",hour.toInt() % 12)
        } else {
            splitString[0] = String.format("%02d",if(hour < 12) hour + 12 else hour)
        }
        return splitString.joinToString(":").dropLast(2)
    }
    
  • + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    #

    Complete the 'timeConversion' function below.

    #

    The function is expected to return a STRING.

    The function accepts STRING s as parameter.

    #

    def timeConversion(s): # Write your code here period=s[-2:] hours,minutes,seconds=map(int,s[:-2].split(':')) if period=='AM' and hours==12: hours=0 elif period=='PM' and hours !=12: hours+=12 return f"{hours:02}:{minutes:02}:{seconds:02}"

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()
    
    result = timeConversion(s)
    
    fptr.write(result + '\n')
    
    fptr.close()
    
  • + 0 comments
    public static String timeConversion(String s) {
    // Write your code here
        String hours = s.substring(0,2) ;
        String restOfTime = s.substring(2,8);
        String meridian = s.substring(8);
    
        if((meridian.equals("PM")) && !hours.equals("12")){
            hours = String.valueOf(Integer.parseInt(hours)+12) ;
            }else if(meridian.equals("AM") && hours.equals("12")){
                hours="00";
            }
    
        return hours + restOfTime;
    }
    
  • + 0 comments

    c++20 (chose readability over latency)

    string timeConversion(string s) {
    
    int nDigits = 2;
    
    auto time = s.substr(0, s.size() - nDigits);
    auto zone = s.substr(s.size() - nDigits);
    int hour = stoi(time.substr(0, nDigits));
    
    std::stringstream ss;
    ss << std::setw(nDigits) << std::setfill('0');
        
    if(zone == "AM"){
        auto amHour = hour%12;
    
        ss << amHour << time.substr(nDigits);
    }
    else{
        
        auto pmHour = hour;
        
        if(hour < 12){
            pmHour = hour + 12;
        }
    
        ss << pmHour << time.substr(nDigits);
    }
    
    return ss.str();
    }
    
  • + 0 comments

    def timeConversion(s): period = s[-2:] #last two digits = pm/am time = s[:-2] #without last two digits = time hms = list(map(int, time.split(':'))) #splits the list and converts str into int in one line

    if period == 'AM':
        if hms[0] == 12:
            hms[0] -= 12
    if period == 'PM':
        if not hms[0] == 12:
            hms[0] += 12
    
    hms = [f"{i:02}" for i in hms] #makes sure only two digits allowed
    return ":".join(hms) #reproducing string