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, 06:30 am in the 12-hour format is equivalent to 06:30 in the 24-hour format, while 06:30 pm in the 12-hour format is equivalent to 18: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 <stdio.h>

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

    // take input
    printf("Enter the time in 12-hour format (hh:mm am/pm): ");
    scanf("%d:%d %c", & h, & 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
        printf("%02d:%02d a.m is same as %02d:%02d in 24-hour format\n", h, m, h1, m1);
    } 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
        printf("%02d:%02d p.m is same as %02d:%02d in 24-hour format\n", h, m, h1, m1);
    }

    return 0;
}

Output


Case 1:

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

05:45 p.m is same as 17:45 in 24-hour format


Case 2:

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

04:30 a.m is same as 04:30 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 <stdio.h>

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

    // take input
    printf("Enter the time in 24-hour format (hh:mm): ");
    scanf("%d:%d", & h, & 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
        printf("%02d:%02d is same as %02d:%02d a.m in 12-hour format\n", h, m, h1, m1);
    } else { // if hour value greater or equal to 12
        // subtract 12 from hour value
        h1 = h1 - 12;
        printf("%02d:%02d is same as %02d:%02d p.m in 12-hour format\n", h, m, h1, m1);
    }

    return 0;
}

Output


Case 1:

Enter the time in 24-hour format (hh:mm): 16:20

16:20 is same as 04:20 p.m in 12-hour format


Case 2:

Enter the time in 24-hour format (hh:mm): 9:30

09:30 is same as 09:30 a.m in 12-hour format