Advertisement Jump to content
Sign in to follow this  
cpp forever

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

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

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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 this post

Link to post
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


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)

So, your output will be:


all calculated with respect only to s.

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

Share this post

Link to post
Share on other sites

seconds = (miliseconds/1000) % 60;
minutes = ((miliseconds/1000)/60) % 60;
hours = ((miliseconds/1000)/60)/60;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!