C++ Program for Conversion Between 12-Hour and 24-Hour Time Format

24hour-12hour

C++ programs for conversion between 12-hour and 24-hour time format have been shown here. The 12-hour time format and the 24-hour time format are two different ways of representing time.


The 12-hour time format is a system in which the day is divided into two 12-hour periods: AM / am (ante meridiem) and PM / pm (post meridiem). In ante meridiem, the hours from 1 to 12 are used to represent the time from midnight to noon, while in post meridiem the hours from 1 to 12 are used to represent the time from noon to midnight. On the other hand, the 24-hour time format is a system in which the day is divided into 24 hours. In this system, the hours from 0 to 23 are used to represent the time from midnight to midnight.


For example, 09:30 am in the 12-hour format is equivalent to 09:30 in the 24-hour format, while 09:30 pm in the 12-hour format is equivalent to 21:30 in the 24-hour format.






1. Program & Output for Conversion Between 12-Hour Time Format and 24-Hour Format




1.1. C++ Program & Output to convert 12 hour time format to 24 hour format

Code has been copied
/******************************
       alphabetacoder.com
 C++ program to convert 12 hour 
 time format to 24 hour format
*******************************/
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    // declare variables
    int h, m, h1, m1;
    char am_pm, separator;

    // take input
    cout << "Enter the time in 12-hour format (hh:mm am/pm): ";
    cin >> h >> separator >> m >> am_pm;

    // minute value will remain same
    m1 = m;
    // copy hour value
    h1 = h;

    // Adjust the hour
    if (am_pm == 'a' || am_pm == 'A') { // if it is am / AM
        // if hour = 12, then it is 0 in 24 hour format
        if (h1 == 12) {
            h1 = 0;
        }
        // display result
        cout << setw(2) << setfill('0') << h << ":" << setw(2) << m <<
            " a.m is same as " << setw(2) << h1 << ":" << setw(2) << m1 <<
            " in 24-hour format" << endl;
    } else if (am_pm == 'p' || am_pm == 'P') { // if it is pm / PM
        // if hour not equals 12, then add 12 in 24 hour format    
        if (h1 != 12) {
            h1 += 12;
        }
        // display result
        cout << setw(2) << setfill('0') << h << ":" << setw(2) << m <<
            " p.m is same as " << setw(2) << h1 << ":" << setw(2) << m1 <<
            " in 24-hour format" << endl;
    }

    return 0;
}

Output


Enter the time in 12-hour format (hh:mm am/pm): 11:45 pm

11:45 p.m is same as 23:45 in 24-hour format





1.2. C++ Program & Output to convert 24 hour time format to 12 hour format

Code has been copied
/*******************************
       alphabetacoder.com
 C++ program to convert 24 hour 
 time format to 12 hour format
********************************/
#include <iostream>

using namespace std;

int main() {
    // declare variables
    int h, m, h1, m1;
    char separator;

    // take input
    cout << "Enter the time in 24-hour format (hh:mm): ";
    cin >> h >> separator >> m;

    // copy hour and minute value
    h1 = h;
    m1 = m;

    // Adjust the hour
    if (h1 < 12) { // if hour value less than 12
        // display the result
        cout << h << ":" << m << " is same as " << h1 << ":" << m1 << " a.m in 12-hour format" << endl;
    } else { // if hour value greater or equal to 12
        // subtract 12 from hour value
        h1 = h1 - 12;
        cout << h << ":" << m << " is same as " << h1 << ":" << m1 << " p.m in 12-hour format" << endl;
    }

    return 0;
}

Output


Enter the time in 24-hour format (hh:mm): 13:45

13:45 is same as 01:45 p.m in 12-hour format