Here's a script: Get years/months/days/hours/seconds

local module = {}
function module:get(timeInSeconds)
	local years   = math.floor(timeInSeconds / 31556926)
	timeInSeconds = timeInSeconds % 31556926

	local months  = math.floor(timeInSeconds / 2629744)
	timeInSeconds = timeInSeconds % 2629744

	local days    = math.floor(timeInSeconds / 86400)
	timeInSeconds = timeInSeconds % 86400

	local hours   = math.floor(timeInSeconds / 3600)
	timeInSeconds = timeInSeconds % 3600

	local minutes = math.floor(timeInSeconds / 60)
	local seconds = timeInSeconds % 60

	local parts = {}

	if years   > 0 then table.insert(parts, years   .. "y") end
	if months  > 0 then table.insert(parts, months  .. "m") end
	if days    > 0 then table.insert(parts, days    .. "d") end
	if hours   > 0 then table.insert(parts, hours   .. "h") end
	if minutes > 0 then table.insert(parts, minutes .. "m") end
	if #parts == 0 then
		return seconds .. "s"
	end

	return table.concat(parts, " ")
end

return module

Last updated:09/06/25

4 Likes

Wouldn’t this be more efficient?

local function timestamp(s:number?):string
   return os.date("%Yy %mm %dd %Hh %Mm %Ss",s)
end
2 Likes

i tested this code but it wasn’t perfectly displaying the date
heres the output

 2y 12m 20d 0h 0m --my code output

 1971y 12m 22d 00h 00m 00s
1 Like