-1

I have a time difference between two datetimes. The interval in seconds is 3660 which means 1 hour and 1 minute ago. How can i show the interval of seconds in hours and minutes like i said before? i can get the hours by doing (3600/60)/60 which gives me the one hour but how do i get the remaining minutes along with the hour?

Any help appreciated.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
stefanosn
  • 3,264
  • 10
  • 53
  • 79

3 Answers3

7

Something like this:

int totalTime = 3660;
int hours = totalTime / 3600;
int minutes = (totalTime % 3600) / 60;
mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
1

Use the modulo operator:

(3660 / 60) % 60 = 1

Example of modulo:

0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
7 % 3 = 1
...

See the pattern here?

Cyrille
  • 25,014
  • 12
  • 67
  • 90
1

You can use the C div function for getting the quotient and remainder with a single function call.

#include <stdio.h>
#include <stdlib.h>  //div_t

int main (int argc, char const *argv[])
{
  int seconds = 3661;
  div_t hrmin, minsec;
  minsec = div(seconds, 60);
  hrmin = div(minsec.quot, 60);

  printf("%i seconds equals %i hours, %i minutes and %i seconds.\n", \
         seconds, hrmin.quot, hrmin.rem, minsec.rem);

  return 0;
}
Thies Heidecke
  • 2,497
  • 1
  • 23
  • 25