Time Conversion

  • + 0 comments

    string timeConversion(string s) { string hour = s.substr(0, 2); // starts from 0 and len = 2 string amPM = s.substr(8); // from 0 to the rest

    string result = "";
    if (hour == "12") {
        result += (amPM == "PM") ? "12" : "00";
    } else {
        result += (amPM == "PM") ? to_string(stoi(hour) + 12) : hour;
    }
    
    result += s.substr(2, 6); // starts from the index 2 and len = 6
    return result;
    

    }