- 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?
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: