Thread: Calculating Time Difference When The Time Is Stored as an int

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Calculating Time Difference When The Time Is Stored as an int

    Right heres what I'm trying to do-

    I'm making a function that recieves a 24 hour time as an int. For example "1300", for one o'clock and "1534", for 26 minutes to 4. What I want to do is get the number of minutes difference between the two values but have problems when I try to just take them away from each other because of the whole 60minutes in an hour problem.

    Is there any way I can seperate the first two digits of the int passed to my function so that I can multiply them by 60 to get a difference that way? I can't use the time function to get the program to do it for me.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    One way to skin that cat...
    Code:
        char str_time[] = "1534";
        char *str_minutes = str_time + 2;
        
        int hour, minute;
    
        minute = atoi(str_minutes);
        str_time[2] = NULL;
        hour = atoi(str_time);
    
        printf("hour = %d\n", hour);
        printf("minute = %d\n", minute);
    gg
    Last edited by Codeplug; 11-15-2003 at 10:18 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Divide it by 100 to get the "hours", mod it by 100 to get the "minutes".

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

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Dammit of course. Thanks quzah got it done. Knew it had to be something really simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM