How would I convert a number of seconds into a HH:MM:SS format?

I’m making a server browser and would like to include the server’s uptime / age (workspace.DistributedGameTime) into readable string, such as:

  • HH:MM:SS
  • 00:32:12
  • 02:10:52
  • etc

I’d really appreciate your help!

You can use a function such as this to first get the string formatting down:
function FormatAgeString(text) return string.format("%02i", text) end

You can use this to format it correctly after you got the hour, minute, and second correctly variabilized in which you can return it again in a function and display anywhere.

Hope this helps :slight_smile:

3 Likes

Thank you for your response!

How would I go about putting the individual variables for hour, minute and seconds into their respective variables?

1 Like

This can be used for hours.

local hours = math.floor(value / 3600)

This for minutes.

local minutes = math.floor(value / 60)

For the seconds, it would just be the value (DistributedGameTime).

You can use the FormatAgeString from my previous post.

After that, you can do it like this.

FormatAgeString(hours) .. "H " .. FormatAgeString(minutes) .. "M " .. FormatAgeString(seconds) .. "S"

You could put that all together and return them in a function, which is easiest.

3 Likes

I’ve ran into a little issue:

The seconds value always stays the same, so I’ve come up with a solution:

function FormatTime(seconds)
    local hours = math.floor(seconds / 3600)
    local minutes = math.floor((seconds % 3600) / 60)
    local secondsRemaining = seconds % 60

    local formattedTime = string.format("%02d:%02d:%02d", hours, minutes, secondsRemaining)
    return formattedTime
end

Even though it did not produce the desired result, your help was still valuable. Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.