#HH:MM:SS Timer

1 messages · Page 1 of 1 (latest)

spring burrow
#

I want to make a timer that counts up in HH:MM:SS Format, but I have no idea how to and I can't find anything helpful online.

woeful compass
# spring burrow I want to make a timer that counts up in HH:MM:SS Format, but I have no idea how...

This is a general coding algorithm, not just in lua. You can search up the pseudocode for it, but basically do it like this:

function formatTime(timeInSeconds)
    local hours = timeInSeconds / 3600 -- 3600 is seconds in an hour
    timeInSeconds %= 3600 -- We use modulo (%) to get the remainder of the division
    local minutes = timeInSeconds / 60 -- 60, of course, is seconds in a minute
    local seconds %= 60 -- The remainder of the division is our seconds
    
    -- Some other formatting stuff here like (if hours == 0, just do "00:00" and etc)

    return hours .. ":" .. minutes .. ":" .. seconds
end

As for the countdown itself, you can do with something like this:

function timer(timeInSeconds)
    local timeLeftInSeconds = 0
  
    while currentTimeInSeconds <= timeInSeconds do
        task.wait(1)
        currentTimeInSeconds += 1

        local formattedTime = formatTime(currentTimeInSeconds)

        -- do something
    end
end
#

So if, for example, we want to count up to 35 minutes and 12 seconds...

35 minutes 12 seconds -> 2112 seconds

2100 / 3600 = 0 hours
2100 % 3600 = 2100
2100 / 60 = 35 minutes
2100 % 60 = 12 seconds

Result (without any other formatting): 0:35:12