C Program to Convert Time from 12 Hour to 24 Hour Format

This is a C Program to convert 12-hour AM-PM time format to 24-hour Military time format.

Problem Description

Given a time in 12-hour AM/PM format, convert it to military (24-hour) time. User has to take input as a string containing a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01 ≤ hh ≤12 and 00 ≤ mm,ss ≤59.

Expected Input and Output

Input:- 09:15:55PM
Output:- 21:15:55

Input:- 12:00:00AM
Output:- 00:00:00

Input:- 03:55:50AM
Output:- 03:55:50

Problem Solution

1. Take input as taken above in sample inputs. (“ %d:%d:%d%s ”, &hh,&mm,&ss,a) or you can provide inputs to ‘hh’, ‘mm’, ‘ss’ and ‘a’ separately.
2. Check and compare if the string ‘a’ at the end of input is ‘AM’ or ‘PM’.
3. Check the value of hh, and solve accordingly.

advertisement
advertisement
Program/Source Code

Here is source code of the C Program for conversion of 12-hour AM-PM time format to 24-hour military time format. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on windows 10. The program output is also shown below.

  1. /* C Program for converting 12-hour time format to 24-hour time format. */
  2. #include<stdio.h>
  3. #include<string.h>
  4. int main()
  5. {
  6.     int hh, mm, ss;
  7.     char a[3];
  8.     printf("Enter hours 'hh' \t");
  9.     scanf("%d", &hh);
  10.     printf("Enter minutes 'mm' \t");
  11.     scanf("%d", &mm);
  12.     printf("Enter seconds 'ss' \t");
  13.     scanf("%d", &ss);
  14.     printf("Enter string 'am' or 'pm' \t");
  15.     scanf("%s", &a);
  16.     /*
  17.      * user is allowed to enter time only in 12-hour format 
  18.      * so that 'hh' cannot be greater than 12.
  19.      */
  20.     if(hh <= 12 && mm <= 59 && ss <= 59)      
  21.     {
  22.         if((strcmp(a,"PM") == 0) || (strcmp(a,"pm") == 0) 
  23.            && (hh != 0) && (hh != 12))
  24.         {
  25.             hh = hh + 12;
  26.         }
  27.         if((strcmp(a,"AM") == 0) || (strcmp(a,"am") == 0) && (hh == 12))
  28.         {
  29.             hh = 0;
  30.         }
  31.         printf("The obtained 24-hour format of input is \t");
  32.         printf("%02d:%02d:%02d", hh, mm, ss);
  33.         printf("\n\n");
  34.     }
  35.     else
  36.     {
  37.         printf("Provide the correct inputs.");
  38.     }
  39.     return 0;
  40. }
Program Explanation

1. The user will give input in 12-hour format, which will contain 4 variables, hh for hours, mm for minutes, ss for seconds and a string ‘a’ for denoting ‘AM’ or ‘PM’.

Note: Join free Sanfoundry classes at Telegram or Youtube

2. After taking the input the user will check if it is ‘PM’ and value of ‘hh’ is anything apart from 00 or 12, it’ll be directly added by 12 and mm,ss will remain same. For example, if the user inputs hh as 05 ‘PM’, in 24-hour format 05 pm = 17 which is nothing but 05+12.

3. But if it is ‘AM’ and value of hh is 12, the value of hh will be made = 0 because after 23rd hour it’ll start again from 00, and for the remaining cases if it is ‘AM’ then the time in 12-hour and 24-hour format will remain same. For example, if the user inputs time as 11:47:56AM the output will be 11:47:56 same as it was in 12-hour format, but if the user enters 12:55:21AM the output will be 00:55:21 because the range of hh’s value is between 00 and 23 both inclusive and immediately after 23rd hour we will be getting 00th hour not 24th.

Runtime Test Cases
1.      Enter hours 'hh'        09
        Enter minutes 'mm'      15
        Enter seconds 'ss'      55
        Enter string 'am' or 'pm'       pm
        The obtained 24-hour format of input is         21:15:55
 
2.	Enter hours 'hh'        12
        Enter minutes 'mm'      00
        Enter seconds 'ss'      00
        Enter string 'am' or 'pm'       am
        The obtained 24-hour format of input is         00:00:00
 
3.	Enter hours 'hh'        03
        Enter minutes 'mm'      55
        Enter seconds 'ss'      50
        Enter string 'am' or 'pm'       am
        The obtained 24-hour format of input is         03:55:50
 
4.	Enter hours 'hh'        23
        Enter minutes 'mm'      13
        Enter seconds 'ss'      11
        Enter string 'am' or 'pm'       am
        Provide the correct inputs.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.