Time Conversion

Sort by

recency

|

5067 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the vidéo here : https://youtu.be/G7bia__Vxqg

    int main() {
        string s;
        cin >> s;
        int h;
        sscanf(s.c_str(), "%d", &h);
        if(s[8] == 'A' && h == 12) h = 0;
        if(s[8] == 'P' && h != 12) h+=12;
        cout << setw(2) << setfill('0') << h;
        cout << s.substr(2, 6);
        return 0;
    }
    
  • + 0 comments

    Solution in Kotlin -

    return if (s.lowercase().contains("pm")) {
            var time = s.replace("PM", "", ignoreCase = true).trim()
            val timeArray = time.split(":").toMutableList()
            val timeInt = timeArray[0].toInt()
    
            timeArray[0] = if (timeInt == 12) {
                "12"
            } else {
                (timeInt + 12).toString()
            }
            timeArray.joinToString(":")
        } else {
            var time = s.replace("AM", "", ignoreCase = true).trim()
            val timeArray = time.split(":").toMutableList()
            val timeInt = timeArray[0].toInt()
    
            timeArray[0] = if (timeInt == 12) {
                "00"
            } else {
                timeInt.toString().padStart(2, '0')
            }
            
            timeArray.joinToString(":")
        }
    
  • + 0 comments

    py:


    def timeConversion(s): if s.endswith('AM'): if s[:2]=='12': g = '00' return g + s[2:-2] return s[:-2] elif s.endswith('PM'): if s[:2]=='12': return (s[:-2]) g = str(int(s[:2])+12) s = g + s[2:-2] return s

  • + 0 comments

    if s[:2] == '12' and s[8:] == 'AM': return '00'+ s[2:8] elif s[:2] == '12' and s[8:] == 'PM': return '12' + s[2:8] elif s[8:]== "AM": return s[0:8] else: return str(int(s[:2])+12)+ s[2:8]

  • + 0 comments
     public static String timeConversion(String s) {
        int shift = s.endsWith("PM") ? 12 : 0;
        int hours = Integer.parseInt(s.substring(0, 2));
        hours = (hours == 12) ? shift : hours + shift;
        return String.format("%02d%s", hours, s.substring(2, 8));
    }