Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


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


Copyright 1999 - 2012