Time Conversion

  • + 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(":")
        }