# Thread: Convert Milliseconds to Hours, Minutes, Seconds

1. ## Convert Milliseconds to Hours, Minutes, Seconds

The title expains! I need to change a integer containing a time value in milliseconds to hours, minutes and seconds but I'm absolutely stuck on the maths!
As one second is 1000 milliseconds I know I can get the seconds simply by dividing by 1000. But to get the hours and minutes too?
I want to display it like "X hours, X minutes and X seconds".
Halp! Thnx.

2. Code:
```int miliseconds = 9999999;
printf("%d hours, %d minutes and %d seconds\n", (int) miliseconds / (1000 * 60 * 60), (int) miliseconds / (1000 * 60), (int) miliseconds / 1000);```

3. Thanks but I think that isn't what I'm looking for.
If it's 1hr and 30mins and 10 seconds away, I want it to say that. Not the total amount of hours, total amount of minutes and total amount of seconds.

Do you get what I mean?

4. I guess what you can do is something like this:

Hours = Milliseconds / (1000*60*60)
Minutes = (Milliseconds % (1000*60*60)) / (1000*60)
Seconds = ((Milliseconds % (1000*60*60)) % (1000*60)) / 1000

I think thats what your trying to say.

5. Thank god, I'd never be able to do that kind of maths. Modulus is not my strong point.
Thanks mate, I'll try it tomorrow.

6. Modulus is simply the remainder after devision, it's a pretty simple concept once you've got your head around it's applications.

Here's how I did this in PHP (baring in mind that `\$dif` would be a second-representation of time, so you'd need to divide your milliseconds by 1000 for this logic to work)

7. 27
posts
Registered User
You didn't say what language, but if you are using c#, i believe you can use the timespan object, put that in for the miliseconds, and it will tell you exactly what you want.