From milliseconds to hour, minutes, seconds and milliseconds

前端 未结 9 1830
失恋的感觉
失恋的感觉 2020-12-07 21:22

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

相关标签:
9条回答
  • 2020-12-07 21:29
    milliseconds = x
    total = 0
    while (milliseconds >= 1000) {
      milliseconds = (milliseconds - 1000)
      total = total + 1
    }
    hr = 0
    min = 0
    while (total >= 60) {
      total = total - 60
      min = min + 1
      if (min >= 60) hr = hr + 1
      if (min == 60) min = 0
    }
    sec = total
    

    This is on groovy, but I thing that this is not problem for you. Method work perfect.

    0 讨论(0)
  • 2020-12-07 21:29

    Arduino (c++) version based on Valentinos answer

    unsigned long timeNow = 0;
    unsigned long mSecInHour = 3600000;
    unsigned long TimeNow =0;
    int millisecs =0;  
    int seconds = 0;
    byte minutes = 0;
    byte hours = 0;
    
    void setup() {
    Serial.begin(9600);
    Serial.println (""); // because arduino monitor gets confused with line 1
    Serial.println ("hours:minutes:seconds.milliseconds:");
    }
    
    void loop() {
    TimeNow = millis(); 
    hours = TimeNow/mSecInHour;
    minutes = (TimeNow-(hours*mSecInHour))/(mSecInHour/60);
    seconds = (TimeNow-(hours*mSecInHour)-(minutes*(mSecInHour/60)))/1000;
    millisecs = TimeNow-(hours*mSecInHour)-(minutes*(mSecInHour/60))-       (seconds*1000);
    
    Serial.print(hours);  
    Serial.print(":");
    Serial.print(minutes);
    Serial.print(":"); 
    Serial.print(seconds); 
    Serial.print("."); 
    Serial.println(millisecs); 
    }
    
    0 讨论(0)
  • 2020-12-07 21:30
    milliseconds = 12884983  // or x milliseconds
    hr = 0
    min = 0
    sec = 0 
    day = 0
    while (milliseconds >= 1000) {
      milliseconds = (milliseconds - 1000)
      sec = sec + 1
      if (sec >= 60) min = min + 1
      if (sec == 60) sec = 0
      if (min >= 60) hr = hr + 1
      if (min == 60) min = 0
      if (hr >= 24) {
        hr = (hr - 24)
        day = day + 1
      }
    }
    

    I hope that my shorter method will help you

    0 讨论(0)
  • 2020-12-07 21:31

    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;
    }
    
    0 讨论(0)
  • 2020-12-07 21:36

    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().

    0 讨论(0)
  • 2020-12-07 21:46

    Just an other java example:

    long dayLength = 1000 * 60 * 60 * 24;
    long dayMs = System.currentTimeMillis() % dayLength;
    double percentOfDay = (double) dayMs / dayLength;
    int hour = (int) (percentOfDay * 24);
    int minute = (int) (percentOfDay * 24 * 60) % 60;
    int second = (int) (percentOfDay * 24 * 60 * 60) % 60;
    

    an advantage is that you can simulate shorter days, if you adjust dayLength

    0 讨论(0)
提交回复
热议问题