# Have seconds, give me %h:%m:%s

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How to convert milliseconds to format '%h:%m:%s'. E.g. I have 90 secs - '00:01:30', 60*60*40+90 secs - '40:01:30'. Maybe using strftime() function?

##### Share on other sites
To get minutes, divide seconds by 60, so (int)90/60 = 1
To get the seconds, get the remainder of 90/60 by using the modulus operator, so 90%60 = 30.

To get hours, hrm, divide seconds by 3600.

Edit: Sorry, I thought you said you had seconds, just convert the milliseconds into seconds by dividing by 1000.

##### Share on other sites
Given s seconds:

m minutes = s/60
h hours = s/3600
ms milliseconds = s*1000

Solving these equations with respect to various variables gives you a way to find any two variables given one variable:

s = m*60
s = h*3600
s = ms/1000

m = (h*3600)/60
m = (ms/1000)/60

h = (m*60)/3600
h = (ms/1000)/3600

etc.

EDIT: Sorry, I think I misinterpreted your question. You have s seconds, and you want to express the total time in the format h/m/s_m, m being relative to h, s_m relative to m, correct?

Well, given s seconds, the number of total hours elapsed is

h = floor(s/3600)

The number of minutes relative to h is then:

m = floor((s - floor(s/3600) * 3600)/60)

And the number of seconds relative to m:

s_m = s - 60 * floor((s - floor(s/3600))/60)

[h]/[m]/[s_m]

all calculated with respect only to s.

[Edited by - nilkn on September 10, 2005 11:18:14 AM]

##### Share on other sites
seconds = (miliseconds/1000) % 60;minutes = ((miliseconds/1000)/60) % 60;hours = ((miliseconds/1000)/60)/60;

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011384
• ### Who's Online (See full list)

There are no registered users currently online

×