Results 1 to 7 of 7

Thread: Convert Milliseconds to Hours, Minutes, Seconds

  1. #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.
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  2. #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);
    If you notice this notice you will notice that this notice is not worth noticing.

    "Are you doing anything tonight? If not, how about me?"

    Opera Sucks! - FIX IT
    Oliver Zheng

  3. #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?
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  4. #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. #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.
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  6. #6
    icio's Avatar
    3,810
    posts
    looks better in lowercase
    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.

  7. #7
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012