Thread: 24-hour time to 12-hour time

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    24-hour time to 12-hour time

    OK I got this so far:

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    int tfhour;
    int tfseconds;
    int twhour;
    
            printf("Enter a 24-hour time (ex. 10:23): ");
            scanf("%d:%d", &tfhour, &tfseconds);
    
            if (tfseconds > 60 || tfseconds < 0)
    			{
    			printf("Not a valid seconds time!\n");
    			return;
    			}
    		
    		if (tfhour <= 12 && tfhour >= 0)
                 printf("Equivalent 12-hour time: %d:%.2d AM\n", tfhour, tfseconds);
                                                                                   
    		else if (tfhour >= 13 && tfhour <= 24)
                {
                 twhour = (tfhour - 12);
                 printf("Equivalent 12-hour time: %d:%d PM\n", twhour, tfseconds);
                }
    
            else
            printf("Not a valid hour!\n");
            return;
    
    }
    I got two questions!
    1) I need to get two zeros on the seconds output instead of a single digit. I know why it is doing it..lol But I need to know how to fix it?

    2) As far as military time is concerned 12:00 is 12:00 pm while 24:00 is 12:00 AM. How can I get that to work out like that? Another if statement maybe?

    Any help would be appreciated!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got a minor issue in your first scanf: You never scan for seconds, just the hour and seconds.

    Otherwise, you've already got it started right, use "%02d" for displaying your two numbers.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Re: 24-hour time to 12-hour time

    How about this:

    Code:
    #include <stdio.h>
    
    main ()
    {
    
    int tfhour;
    int tfseconds;
    int twhour;
    
            printf("Enter a 24-hour time (ex. 10:23): ");
            scanf("%d:%d", &tfhour, &tfseconds);
    
            if (tfseconds > 60 || tfseconds < 0)
    			{
    			printf("Not a valid seconds time!\n");
    			return;
    			}
    		
    		if(tfhour<0 || tfhour>24) printf("Not a valid hour!\n");
    		else {
    		  printf("Equivalent 12-hour time: %d:%.2d ", tfhour, tfseconds);
    		  if(tfhour/12 == 1) printf("PM\n"); else printf("AM\n");
    		}
            return;
    
    }

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
     if (hour > 12)
     {
       hour -= 12;
       pm = 1;
     }
    sorted.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    OK after reading through everyones suggestions I came up w/ this:
    Code:
    #include <stdio.h>
    
    main ()
    {
    
    int tfhour;
    int tfseconds;
    int twhour;
    
            printf("Enter a 24-hour time (ex. 10:23): ");
            scanf("%d:%d", &tfhour, &tfseconds);
    	
    	if (tfseconds > 60 || tfseconds < 0)
    			{
    			printf("Not a valid seconds time!\n");
    			return;
    			}
    	
    	if(tfhour <= 0 || tfhour > 24) 
    		{
    		printf("Not a valid hour!\n");
            return;
    		}
    
    	else if (tfhour <= 12 && tfhour > 0)
    			twhour = tfhour;
    			                                                                              
    	else if (tfhour >= 13 && tfhour <= 24)
    			twhour = (tfhour - 12);
    	
    	printf("Equivalent 12-hour time: %d:%.2d ", twhour, tfseconds);
    
        if (tfhour / 12 == 1) 
    		printf("PM\n"); 
    		
    	else 
    		printf("AM\n");
    
            return;
    }
    It seems to work, but seeing if maybe the experts can find something else wrong w/ it?...lol I'm not looking for effiecent here...just making sure it works!

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    To make it more efficient you could replace that:
    Code:
    else if (tfhour <= 12 && tfhour > 0)
    			twhour = tfhour;
    			                                                                              
    	else if (tfhour >= 13 && tfhour <= 24)
    			twhour = (tfhour - 12);
    with that:

    Code:
    twhour=(tfhour-1)%12+1;

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Just as a note, 00:00 is valid to use instead of 24:00

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >As far as military time is concerned 12:00 is 12:00 pm while 24:00 is 12:00 AM.

    Isn't 00:00 the equivalent of 12:00 AM? And 00:30 would be valid but 24:15 invalid?

    >main ()

    FAQ

    >To make it more efficient you could replace [...]

    Efficiency is difficult to generalize. I have seen a one-liner that took orders of magnitude more time than its multiline counterpart that used temporary variables. In this example, if I were interested in (speed) efficiency, I might be a little more shy about doing a division because my typical implementation would call a library routine. YMMV.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    You might try for efficiency/readability:

    Code:
        . . . .
    	else if (tfhour <= 12 && tfhour > 0)
            {
                twhour = tfhour;
                ampm = 0;
            }                                             
    	else if (tfhour >= 13 && tfhour <= 24)
            {
                twhour = (tfhour - 12);
                ampm = 1;
            }                                             
    
            printf("Equivalent 12-hour time: %d:%.2d ", twhour, tfseconds);
    
            if (ampm)  printf("PM\n"); 
    	    else   printf("AM\n");
    
            return;
    }
    Also, why hours and seconds? I've always used hours and minutes
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  2. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  3. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM
  4. Can't figure out why?
    By kwigibo in forum C Programming
    Replies: 10
    Last Post: 10-14-2001, 10:58 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM