• How to convert 24 hour time to 12 hour in C++

    Amit Member

    How to convert 24 hour time to 12 hour in C++

  • SapnaVishwas Member

    The problem one is going from string format in one way to a string format in another.

    If(hh >=12 )
        {
            suffix = "PM";
         hh=   (hh>12) ? hh-12:hh;
        }
        else
        {
            suffix = "AM";
        }
    
  • ShikhaTan Member

    If high performance is an issue, then you would want to improve on this.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    string change2(string time24Hour)
    {
       char buf[3];
       int hh = atoi(time24Hour.c_str());
       string suffix = hh > 11 ? "PM" : "AM";
       hh = hh > 12 ? hh - 12 : hh;
       sprintf(buf, "%02d", hh);
    
       return string(buf) + time24Hour.substr(2, string::npos) +  " " + suffix;
    }
    
  • Adan Member

    Regardless of the semantics of what one calls “pure” C++ code, the proposed answer produces “00 AM” and “00 PM” instead of “12 AM” and “12 PM”
    This can be corrected with

            oss << std::setw(2) << std::right << std::setfill('0') << (hour+11)%12+1;
            time24Hour.replace(0,2, oss.str());
            time24Hour += hour>=12?" PM":" AM";
    
Viewing 3 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish