From milliseconds to hour, minutes, seconds and milliseconds

120,311

Solution 1

Here is how I would do it in Java:

int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours   = (int) ((milliseconds / (1000*60*60)) % 24);

Solution 2

Good question. Yes, one can do this more efficiently. Your CPU can extract both the quotient and the remainder of the ratio of two integers in a single operation. In <stdlib.h>, the function that exposes this CPU operation is called div(). In your psuedocode, you'd use it something like this:

function to_tuple(x):
    qr = div(x, 1000)
    ms = qr.rem
    qr = div(qr.quot, 60)
    s  = qr.rem
    qr = div(qr.quot, 60)
    m  = qr.rem
    h  = qr.quot

A less efficient answer would use the / and % operators separately. However, if you need both quotient and remainder, anyway, then you might as well call the more efficient div().

Solution 3

Maybe can be shorter an more elegant. But I did it.

public String getHumanTimeFormatFromMilliseconds(String millisecondS){
    String message = "";
    long milliseconds = Long.valueOf(millisecondS);
    if (milliseconds >= 1000){
        int seconds = (int) (milliseconds / 1000) % 60;
        int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
        int hours = (int) ((milliseconds / (1000 * 60 * 60)) % 24);
        int days = (int) (milliseconds / (1000 * 60 * 60 * 24));
        if((days == 0) && (hours != 0)){
            message = String.format("%d hours %d minutes %d seconds ago", hours, minutes, seconds);
        }else if((hours == 0) && (minutes != 0)){
            message = String.format("%d minutes %d seconds ago", minutes, seconds);
        }else if((days == 0) && (hours == 0) && (minutes == 0)){
            message = String.format("%d seconds ago", seconds);
        }else{
            message = String.format("%d days %d hours %d minutes %d seconds ago", days, hours, minutes, seconds);
        }
    } else{
        message = "Less than a second ago.";
    }
    return message;
}

Solution 4

not really eleganter, but a bit shorter would be

function to_tuple(x):
   y = 60*60*1000
   h = x/y
   m = (x-(h*y))/(y/60)
   s = (x-(h*y)-(m*(y/60)))/1000
   mi = x-(h*y)-(m*(y/60))-(s*1000)

   return (h,m,s,mi)
Share:
120,311

Related videos on Youtube

Mads Skjern
Author by

Mads Skjern

Computer science student at Aarhus University. Likes Python.

Updated on July 09, 2022

Comments

  • Mads Skjern
    Mads Skjern almost 2 years

    I need to go from milliseconds to a tuple of (hour, minutes, seconds, milliseconds) representing the same amount of time. E.g.:

    10799999ms = 2h 59m 59s 999ms

    The following pseudo-code is the only thing I could come up with:

    # The division operator below returns the result as a rounded down integer
    function to_tuple(x):
        h = x / (60*60*1000)
        x = x - h*(60*60*1000)
        m = x / (60*1000)
        x = x - m*(60*1000)
        s = x / 1000
        x = x - s*1000
        return (h,m,s,x)
    

    I'm sure it must be possible to do it smarter/more elegant/faster/more compact.

    • fvu
      fvu almost 12 years
      you could use the modulo operator ( % in C and friends ) to slightly simplify the calculations of x ( eg x = x % (60*60*1000) )
    • Michał Šrajer
      Michał Šrajer almost 12 years
      Make sure you don't have such functionality already in standard library of language you use.
  • Michał Šrajer
    Michał Šrajer almost 12 years
    it's worth to use TimeUnit in java to make code more readable.
  • JBoy
    JBoy over 9 years
    long millis = 12884983; System.out.println(((millis / (1000 * 60)) % 60)); System.out.println(java.util.concurrent.TimeUnit.MILLISECOND‌​S.toMinutes(millis))‌​; output: 34 | 214