Time Conversion

Sort by

recency

|

848 Discussions

|

  • + 0 comments

    //Language = C++ //Approach

    Extract the A or P from the given time string. A and P lies at second last index of string. string.size() - 2. Remove AM/PM from the given string.

    If the time is given in AM (Then only 12 AM will need to be addressed)

    If time is given in PM - Then there are two cases: i. When time is from 01 PM - 11 PM (Simply add 12 in the hrs) ii. When time is 12 PM (Skip this time as it is already in 24 hrs format)

    string timeConversion(string s) { int n = s.size(); bool isAM = (s.substr(n - 2, 1) == "A") ? true : false; s = s.substr(0, n-2);

    if(isAM){
        if(s[1] == '2'){
            s[0] = '0';
            s[1] = '0';
        }
    } else{
        //Time from 10 PM - 12 PM
        if(s[0] - '0'){
            //12 PM
            if(s[1] - '0' < 2){
                string hrs = to_string((s[1] - '0') + 10 + 12);
                s[0] = hrs[0];
                s[1] = hrs[1];
            }
        } else{
            //Time from 01 PM - 09 PM
            string hrs = to_string((s[1] - '0') + 12);
            s[0] = hrs[0];
            s[1] = hrs[1];
        }
    
    }
    return s.substr();
    

    }

  • + 0 comments

    Split the time and AM/PM into two variables initally. Foucus on implementing logic to handle the two examples that are provided!

  • + 0 comments

    java

    if(s.charAt(8)=='A'){
            if(Integer.parseInt(s.substring(0,2))==12){
                return "00"+s.substring(2,8);
            }
            return s.substring(0, 8);
        }else{
            if(Integer.parseInt(s.substring(0,2))==12){
                return "12"+s.substring(2,8);
            }
            return (12+Integer.parseInt(s.substring(0,2))+s.substring(2,8));
        }
    
  • + 0 comments

    My code of JS/TS

    function timeConversion(s: string): string { // 07:05:45PM
        const isPM = s.endsWith('PM')
        let hh = Number(s.slice(0,2))
        
        if (isPM && hh < 12) { // handle 0-11 hour in PM 
            hh = hh + 12
        } else if (!isPM && hh === 12) { // handle AM hh === 12 cases
            hh = 0
        } 
        const retrunTime = (hh <= 9 ? '0' : '') + hh.toString() + s.slice(2,8) // 19:05:45
        return retrunTime
    }
    
  • + 0 comments

    import java.io.* import java.math.* import java.security.* import java.text.* import java.util.* import java.util.concurrent.* import java.util.function.* import java.util.regex.* import java.util.stream.* import kotlin.collections.* import kotlin.comparisons.* import kotlin.io.* import kotlin.jvm.* import kotlin.jvm.functions.* import kotlin.jvm.internal.* import kotlin.ranges.* import kotlin.sequences.* import kotlin.text.*

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

    fun timeConversion(s: String): String { var ret ="" // Write your code here if(s.contains("PM")){ var time = s.split(":")

          var cal = time[0].toInt()
          if(cal<12){
            cal = cal+12
          }
    
          var calctime = cal.toString()+":"+time[1]+":"+time[2]
            var calctimewithoutPm = calctime.split("PM")
    ret = calctimewithoutPm[0].toString()
    

    } else{ var timeWithoutAm = s.split("AM") var cal =timeWithoutAm[0] var time =cal.toString().split(":") var zero = time[0] if(time[0].toInt()>=12){ zero = ((zero.toInt())-12).toString() if(zero.toInt()==0){ zero="00" } }

      var calctime = zero.toString()+":"+time[1]+":"+time[2]
           // var calctimewithoutPm = calctime.split("PM")
            ret = calctime
    

    } return ret }

    fun main(args: Array) { val s = readLine()!!

    val result = timeConversion(s)
    
    println(result)
    

    }