C Program To Convert 24 Hour Time To 12 Hour Time

For example -

Input : 20:35:20

Output : 8:35:20 PM

Input : 00:15:40

Output : 12:15:40 AM

Algorithm

The time displayed as 00:00:00 in 24-hour clock format corresponds to 12:00:00 in the 12-hour format. It is important to note that the minutes and seconds remain constant in both representations, with the variation occurring solely in the hours due to the shift in meridian.

To verify the hours, we will initially convert the input string representing hours into an integer. Following this conversion, we will calculate the remainder after dividing the hour by 12 to represent it in the 12-hour format.

The scenario where the hour contains 00 will be treated as a distinct case.

C code Implementation

Example

#include <stdio.h>
#include <string.h>
void convert12(char* str) // Take the time array
{
    int h1 = (int)str[0] - '0'; // Get the first digit of hour in 24 hour format
    int h2 = (int)str[1] - '0'; // Get the second digit of hour in 24 hour format
    int hh = h1 * 10 + h2; // Caluclate hour as an integer value
    char Meridien[2]; // Make the AM or PM array
    if (hh < 12) { // If the time is less than 12 hour it is AM
        Meridien[0] = 'A';
        Meridien[1] = 'M';
    }
    else {
        Meridien[0] = 'P'; // When the time is 24 hour it is PM
        Meridien[1] = 'M';
    }

    hh %= 12; // Calculate the 12 hour time

    // Handle 00 and 12 case separately
    if (hh == 0) {
        printf("12"); // Base format as 00 in 24-hour is 12 in 12-hour

        // Printing minutes and seconds
        for (int i = 2; i < 8; ++i) {
            printf("%c", str[i]);
        }
    }
    else {
        printf("%d", hh);
        // Printing minutes and seconds
        for (int i = 2; i < 8; ++i) {
            printf("%c", str[i]);
        }
    }

    // After time is printed Meridien
    printf(" %c%c", Meridien[0], Meridien[1]);
}

int main()
{

    char str[8] = "17:35:20"; // array containing the time

    convert12(str); // Call the function

    return 0;
}

Output:

Output

12%c[value]%c %c%c

Code Explanation

  • Take the char str into the function.
  • Obtain the first digit and second digit of the hour from the 24-hour format string. The '0' converts the string value to an integer.
  • Example
    
    int h1 = (int)str[0] - '0'; // Get the first digit of hour in 24 hour format
    int h2 = (int)str[1] - '0'; // Get the second digit of hour in 24 hour format
    

Once we have extracted each digit representing the hour, we will proceed to convert it into a numerical value.

Example

int hh = h1 * 10 + h2; // Caluclate hour as an integer value
  • Verify whether the time is in a 12-hour or 24-hour format by using an if-else statement, and then assign the Meridian as either AM or PM accordingly.
  • Example
    
    if (hh < 12) { // If the time is less than 12 hour it is AM
           Meridien[0] = 'A';
           Meridien[1] = 'M'
    }
    else {
           Meridien[0] = 'P'; // When the time is 24 hour it is PM
            Meridien[1] = 'M';
              }
    
  • Calculate the remainder when dividing hh by 12, then represent the time in a 12-hour format.
  • Example
    
    hh %= 12; // Calculate the 12 hour time
    
  • We will handle the 00 in 24-hour separately as it is the same as 12 in 12-hour format.
  • After printing the hour print the minutes and seconds
  • Print the minutes and seconds if it is not the 00 case
  • Print the meridian of 12-hour format.

Input Required

This code uses input(). Please provide values below: