Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Write a function that returns the time of day after x amount of seconds has elapsed (?)

Eg

("13:00:00", 56) --> "13:00:56"
("12:00:00", 185) --> "12:02:05"
("12:00:00", 3660) --> "13:01:00"
("13:00:00", 86400) --> "13:00:00"

You can assume that each day is a perfect 86,400 seconds long. Fastest solution wins.

2 answers

function SecondsToClock(seconds)
  local seconds = tonumber(seconds)

  if seconds <= 0 then
    return "00:00:00";
  else
    -- .f = percent zero two point f
    -- URL Encoding fricks it
    local hours = string.format(".f", math.floor(seconds/3600));
    local mins = string.format(".f", math.floor(seconds/60 - (hours*60)));
    local secs = string.format(".f", math.floor(seconds - hours*3600 - mins *60));
    return hours..":"..mins..":"..secs
  end
end

function stringToSeconds(str)
  local Time = string.split(str, ":")
  local hours, minutes, seconds = Time[1], Time[2], Time[3]
  --hours, minutes, seconds
  local hoursSeconds = math.floor(hours*3600)
  local minutesSeconds = math.floor(hours*60)
  return hoursSeconds + minutesSeconds + seconds
end

function addSecondsToString(str, num)
  local inSeconds = stringToSeconds(str)
  return SecondsToClock(inSeconds + num)
end

5 comments

0

SH fricks up the string.format thing :thonk:

0

\

0

%\02

0

\

0

%2502

IAmMew24's solution, posted on his behalf

function totime(s)
	local seconds = s
	local minutes = seconds/60
	local hours = minutes/60
	local seconds1 = seconds-(math.floor(minutes)*60)
	local minutes1=minutes-(math.floor(hours)*60)
	return tostring(math.floor(hours)) .. ":" .. tostring(math.floor(minutes1)) .. ":" .. tostring(math.floor(seconds1))
end
function timeafterseconds(h,m,s,s2)
	local sc = (h*3600)+(m*60)+s+s2
	return totime(sc)
end
print(timeafterseconds(04,30,32,01))

This discussion is preserved as it was. New answers, comments, and votes are closed.