Hello everyone! I have two very useful resource for converting time ( hour : minute : second ) into seconds ( 12345 ) and also converting seconds ( 12345 ) into time ( hour : minute : second ). So first, if you want to convert seconds into time, I will give four variables day, hour, time, seconds. If you don’t want day, you can just set it as 0.
local minutes = 0
local hours = 0
local days = 0
local seconds = 12345
repeat -- a loop
if seconds >= 60 then -- if seconds is more than 60
seconds = seconds - 60 -- we take away 60 from the second
minutes = minutes + 1 -- and we add one to the minute
end
until seconds < 60 -- repeat doing this until seconds is less than 60
repeat -- another loop
if minutes >= 60 then -- if minutes is more than 60
minutes -= 60 -- we take away 60 from the minute
hours += 1 -- and we add one to the hour
end
until minutes < 60 -- repeat doing this until minute is less than 60
repeat -- another loop (idk why im even explaining this)
if hours >= 24 then -- if hours is more than 24
hours -= 24 -- we take away 24 from the hour
days += 1 -- and we add one to the day
end
until hours < 24 -- repeat doing this until hour is less than 24
Now you have four variables : days, hours, minutes, seconds. You can customize your string anyway you like. But the most common one would be m:s. You can have hours or days but it’s uncommon so here’s how you can do it:
if days < 10 then
days = "0"..tostring(days)
end
if days == "00" then
days = ""
end
if hours < 10 then
hours = ":0"..tostring(hours )
end
if hours == ":00" then
hours = ""
end
if minutes < 10 then
minutes = ":0"..tostring(minutes )
end
if minutes == ":00" then
minutes = ""
end
if seconds < 10 then
seconds = ":0"..tostring(seconds )
end
if seconds == ":00" then
seconds = ""
end
local finalTime = days..hours..minutes..seconds
We’re finally done! Now, can make a function out of this:
local function secondsToTime (seconds)
local minutes = 0
local hours = 0
local days = 0
local seconds = 12345
repeat -- a loop
if seconds >= 60 then -- if seconds is more than 60
seconds = seconds - 60 -- we take away 60 from the second
minutes = minutes + 1 -- and we add one to the minute
end
until seconds < 60 -- repeat doing this until seconds is less than 60
repeat -- another loop
if minutes >= 60 then -- if minutes is more than 60
minutes -= 60 -- we take away 60 from the minute
hours += 1 -- and we add one to the hour
end
until minutes < 60 -- repeat doing this until minute is less than 60
repeat -- another loop (idk why im even explaining this)
if hours >= 24 then -- if hours is more than 24
hours -= 24 -- we take away 24 from the hour
days += 1 -- and we add one to the day
end
until hours < 24 -- repeat doing this until hour is less than 24
if not days then
days = 0
end
if not hours then
hours = 0
end
if not minutes then
minutes = 0
end
if not seconds then
seconds = 0
end
if days < 10 then
days = "0"..tostring(days)
end
if days == "00" then
days = ""
end
if hours < 10 then
hours = ":0"..tostring(hours )
end
if hours == ":00" then
hours = ""
end
if minutes < 10 then
minutes = ":0"..tostring(minutes )
end
if minutes == ":00" then
minutes = ""
end
if seconds < 10 then
seconds = ":0"..tostring(seconds )
end
if seconds == ":00" then
seconds = ""
end
local finalTime = days..hours..minutes..seconds
return finalTime
There! Now we can say
print(secondsToTime(12345)) -- 3:25:45
If you want to convert time ( d : h : m : s ) into seconds, you can use the following:
function timeToSeconds (d,h,m,s)
if d == nil then d = 0 end
if h == nil then h = 0 end
if m == nil then m = 0 end
if s == nil then s = 0 end
return ((((d * 24 + h)*60) + m)*60) + s
end
And so it will return a number which is the seconds there are in the time. You can say:
print(timeToSeconds(0,4,39,25)) -- 16765
There! Now you know how to convert seconds into time, time into seconds, add own customization to time and seconds and all that. For more help, join my discord channel. Gave a great day!