timelualua-4.0

Lua 4 script to convert seconds elapsed to days, hours, minutes, seconds


I need a Lua 4 script that converts seconds elapsed since seconds = 0 into a D:HH:MM:SS formatted string. The methods I've looked at try to convert the number to a calendar date and time, but I just need the elapsed time since 0. It's okay if the day value increments into the hundreds or thousands. How do I write such a script?


Solution

  • This is similar to the other answers, but is shorter. The return line uses a format string in to display the result in D:HH:MM:SS format.

    function disp_time(time)
      local days = floor(time/86400)
      local hours = floor(mod(time, 86400)/3600)
      local minutes = floor(mod(time,3600)/60)
      local seconds = floor(mod(time,60))
      return format("%d:%02d:%02d:%02d",days,hours,minutes,seconds)
    end