Basically, i am trying to convert a time number (which is in secends) into a different unit of time.
For an example, seconds to days, or seconds to weeks, etc
if CommandAge < 60 then -- Check if under a minute
AgeText = CommandAge .. " seconds ago"
elseif CommandAge < 3600 then -- Check if under an hour
AgeText = math.floor(CommandAge / 60) .. " minutes ago"
elseif CommandAge < 86400 then -- If under a day
AgeText = math.floor(CommandAge / 3600) .. " hours ago"
elseif CommandAge < 604800 then -- if under a week
AgeText = math.floor(CommandAge / 86400) .. " days ago"
else
AgeText = math.floor(CommandAge / 604800) .. " weeks ago"
end
This works, but is messy and kind of annoying to expand.
local CommandAge = 199929
local age_thresholds = {
{threshold = 60, unit = "second", divisor = 1},
{threshold = 3600, unit = "minute", divisor = 60},
{threshold = 86400, unit = "hour", divisor = 3600},
{threshold = 604800, unit = "day", divisor = 86400},
{threshold = math.huge, unit = "week", divisor = 604800}
}
local ageText = ""
for _, v in ipairs(age_thresholds) do
if CommandAge < v.threshold then
local value = v.divisor and (CommandAge // v.divisor) or CommandAge
ageText = string.format("%d %ss ago", value, v.unit)
print(ageText)
break
end
end
The divisor should be the previous threshold (or 1)
You can’t really calculate it with a function since the thresholds / divisors are arbitrarily chosen values to fit time. (60 * 60 * 24 * 7 gives weeks)
Adapting the code @DiscoDino01 made gives something like this:
local CommandAge = 199929
local age_thresholds = {
{threshold = 60, unit = "second"},
{threshold = 3600, unit = "minute"},
{threshold = 86400, unit = "hour"},
{threshold = 604800, unit = "day"},
{threshold = math.huge, unit = "week"}
}
local ageText = ""
for i, v in age_thresholds do
if CommandAge >= v.threshold then continue end
local divisor = age_thresholds[i-1] and age_thresholds[i-1].threshold or 1
local value = CommandAge // divisor
ageText = string.format("%d %ss ago", value, v.unit)
print(ageText)
break
end