• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Converting lua time into a readable format.

Xikini

I whore myself out for likes
Senator
Premium User
Joined
Nov 17, 2010
Messages
6,798
Solutions
581
Reaction score
5,361
Is there a better way to do it then this?

Basically the idea is to take days/hours/minutes/seconds and only show the bare-minimum information.

Example 1: 4 days, 5 hours, 3 minutes, 2 seconds
Shows -> 4 days 5 hours 3 minutes 2 seconds

Example 2: 0 days, 0 hours, 7 minutes, 2 seconds
Shows -> 7 minutes 2 seconds

Example 3: 1 days, 0 hours, 0 minutes, 0 seconds
Shows -> 1 day 0 hours 0 minutes 0 seconds

Example 3: 0 days, 0 hours, 0 minutes, 6 seconds
Shows -> 6 seconds

Helping someone here, I came up with this function (below), but I want to know if there is a better way to do this?

Lua:
local function convert_seconds_into_readable_text(value)
    local difference_in_seconds = value
    local days, hours, minutes, seconds = 0, 0, 0, 0
    
    while difference_in_seconds ~= 0 do
        if difference_in_seconds >= 86400 then
            difference_in_seconds = difference_in_seconds - 86400
            days = days + 1
        elseif difference_in_seconds >= 3600 then
            difference_in_seconds = difference_in_seconds - 3600
            hours = hours + 1
        elseif difference_in_seconds >= 60 then
            difference_in_seconds = difference_in_seconds - 60
            minutes = minutes + 1
        else
            seconds = difference_in_seconds
            difference_in_seconds = 0
        end
    end
    
    local text = ""
    if days > 0 then
        text = text .. days .. (days == 1 and " day " or " days ") .. hours .. " hours " .. minutes .. " minutes and " .. seconds .. " seconds"
    elseif hours > 0 then
        text = text .. hours .. " hours " .. minutes .. " minutes and " .. seconds .. " seconds"
    elseif minutes > 0 then
        text = text .. minutes .. " minutes and " .. seconds .. " seconds"
    else
        text = text .. seconds .. " seconds"
    end
    return text
end
 
Last edited:
Solution
Is there a better way to do it then this?

Basically the idea is to take days/hours/minutes/seconds and only show the bare-minimum information.

Example 1: 4 days, 5 hours, 3 minutes, 2 seconds
Shows -> 4 days 5 hours 3 minutes 2 seconds

Example 2: 0 days, 0 hours, 7 minutes, 2 seconds
Shows -> 7 minutes 2 seconds

Example 3: 1 days, 0 hours, 0 minutes, 0 seconds
Shows -> 1 day 0 hours 0 minutes 0 seconds

Example 3: 0 days, 0 hours, 0 minutes, 6 seconds
Shows -> 6 seconds

Helping someone here, I came up with this function (below), but I want to know if there is a better way to do this?

Lua:
local function convert_seconds_into_readable_text(value)
    local difference_in_seconds = value
    local days, hours, minutes, seconds...
Is there a better way to do it then this?

Basically the idea is to take days/hours/minutes/seconds and only show the bare-minimum information.

Example 1: 4 days, 5 hours, 3 minutes, 2 seconds
Shows -> 4 days 5 hours 3 minutes 2 seconds

Example 2: 0 days, 0 hours, 7 minutes, 2 seconds
Shows -> 7 minutes 2 seconds

Example 3: 1 days, 0 hours, 0 minutes, 0 seconds
Shows -> 1 day 0 hours 0 minutes 0 seconds

Example 3: 0 days, 0 hours, 0 minutes, 6 seconds
Shows -> 6 seconds

Helping someone here, I came up with this function (below), but I want to know if there is a better way to do this?

Lua:
local function convert_seconds_into_readable_text(value)
    local difference_in_seconds = value
    local days, hours, minutes, seconds = 0, 0, 0, 0
   
    while difference_in_seconds ~= 0 do
        if difference_in_seconds >= 86400 then
            difference_in_seconds = difference_in_seconds - 86400
            days = days + 1
        elseif difference_in_seconds >= 3600 then
            difference_in_seconds = difference_in_seconds - 3600
            hours = hours + 1
        elseif difference_in_seconds >= 60 then
            difference_in_seconds = difference_in_seconds - 60
            minutes = minutes + 1
        else
            seconds = difference_in_seconds
            difference_in_seconds = 0
        end
    end
   
    local text = ""
    if days > 0 then
        text = text .. days .. (days == 1 and " day " or " days ") .. hours .. " hours " .. minutes .. " minutes and " .. seconds .. " seconds"
    elseif hours > 0 then
        text = text .. hours .. " hours " .. minutes .. " minutes and " .. seconds .. " seconds"
    elseif minutes > 0 then
        text = text .. minutes .. " minutes and " .. seconds .. " seconds"
    else
        text = text .. seconds .. " seconds"
    end
    return text
end

Lua:
local function convertSeconds(time)
    local days = math.floor(time / 86400)
    local hours = math.floor(math.fmod(time, 86400) / 3600)
    local minutes = math.floor(math.fmod(time, 3600) / 60)
    local seconds = math.floor(math.fmod(time, 60))
    
    local s = tostring(days > 0 and days .. (days == 1 and " day, " or " days, ") or "")
    s = s .. tostring(hours > 0 and hours .. (hours == 1 and " hour, " or " hours, ") or "")
    s = s .. tostring(minutes > 0 and minutes .. (minutes == 1 and " minute, " or " minutes, ") or "")
    s = s .. tostring(seconds > 0 and seconds .. (seconds == 1 and " second, " or " seconds, ") or "")
    
    return string.gsub(s, ",[^,]*$", "")
end

local value = convertSeconds(122)
print(value)
 
Solution
Back
Top