Results 1 to 7 of 7
February 19th, 2008, 11:47 AM #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".
February 19th, 2008, 01:29 PM #2Code:
int miliseconds = 9999999; printf("%d hours, %d minutes and %d seconds\n", (int) miliseconds / (1000 * 60 * 60), (int) miliseconds / (1000 * 60), (int) miliseconds / 1000);
February 19th, 2008, 03:16 PM #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?
February 19th, 2008, 06:47 PM #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.
February 19th, 2008, 07:06 PM #5
February 19th, 2008, 08:04 PM #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)
Edit: Ooops, link: http://kirupa.com/forum/showthread.php?t=256343"60% of the time it works... every time." -- Paul Rudd as Brian Fantana.
March 6th, 2008, 04:30 PM #727Registered 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.