2

How I can convert milliseconds to minutes or hours, in Objective-C or C?

jscs
  • 63,694
  • 13
  • 151
  • 195
Maxime
  • 3,167
  • 7
  • 26
  • 33

4 Answers4

10

Just use simple division. Use floating point numbers so that you don't lose precision from rounding.

float milliseconds = 966000.0;
float seconds = milliseconds / 1000.0;
float minutes = seconds / 60.0;
float hours = minutes / 60.0;
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
3
#define MSEC_PER_SEC    1000L
#define SEC_PER_MIN     60
#define MIN_PER_HOUR    60

int msec = 966000;
int min  = msec / (MSEC_PER_SEC * SEC_PER_MIN);
int hr   = min  / (MIN_PER_HOUR);
makes
  • 6,438
  • 3
  • 40
  • 58
2

sounds a bit like a joke but what the heck…

  • divide by 60*1000… for minutes.
  • divide by 60*60*1000… for hours.

the beauty of it it works in all programming languages.

machunter
  • 967
  • 1
  • 11
  • 27
1

Divide by 1000 to get seconds. Divide seconds by 60 to get minutes.

Paul R
  • 208,748
  • 37
  • 389
  • 560