How to convert seconds to hⓂs and d:h:m:s

Then how do I add data and months to this script

local function toHMS(s)
	return ("%02i:%02i:%02i"):format(s/60^2, s/60%60, s%60)
end

local seconds = 283
toHMS(seconds)

Do you understand how that script works? The numbers on the right are the important bits.
s is seconds. s%60 on the far right gets the remainder after dividing by 60. (60 obviously is the number of seconds in a minute)
61/60 is 1 with a remainder of 1 (1 minute 1 second)
90/60 is 1 with a remainder of 30 (1 minute 30 seconds)
130/60 is 2 with a remainder of 10 (2 minutes 10 seconds)

The second expression from the right is s/60%60. s/60 is evaluated first under order of operations.
90/60 = 1.5 and 1.5/60 = 0 with a remainder of 1.5 (1.5 minutes, 0 seconds) (string.format rounds down, so it becomes 1)
10000/60 = 166.66, 166.66/60 = 2 with a remainder of 46.66 (2 hours, 46.66 minutes)

And then hours is the same except for the exponent. That expression can be rewritten as this.
s/60/60

I’m not going to show you how to do months because that information already exists on the thread. I’m not going to write scripts for you because it’s against the rules. If you don’t understand something in particular, feel free to ask though.

Practice, but mostly Google. Go ahead and look this up, because it’s all been asked before. Here’s the first result I found, but there’s others if you want to shop around for explanations.

2 Likes

All I am asking is how do I add years and months to the code

There are 24 hours in a day and 365.25 days in a year. Months are 365.25/12 days long.
So just keep converting. 1 second divided by 60 gives you minutes, divided by 60 gives you hours, divided by 24 gives you days, and divide by (365.25/12) to give you months. Divide that by 12 to get years. That’s how conversion works, it’s just a number divided or multiplied by another number.

If you can’t read the information that has already been provided to you in this thread, then maybe you shouldn’t be scripting.

I am pretty experienced as I have made a priority system for overhead ui and I have made a global chat system

That is the opposite of what you would want to do. There are 60 seconds in 1 minute not 60 minutes in 1 second.

Minutes = math.floor(s / 60)
Hours = math.floor(Minutes / 60)
Days = math.floor(Hours / 24)

local function Format(Int)
	return string.format("%02i", Int)
end

local function convertToHMS(Seconds)
	local Minutes = (Seconds - Seconds%60)/60
	Seconds -= Minutes*60
	local Hours = (Minutes - Minutes%60)/60
	Minutes -= Hours*60
	return Format(Hours)..":"..Format(Minutes)..":"..Format(Seconds)
end

This will incorrectly show hours for big numbers. You need to do modulo 24.
Fixed function:

local function toDHMS(s)
	return ("%i:%02i:%02i:%02i"):format(s/60^2/24, s/60^2%24, s/60%60, s%60)
end

sorry for replying after 2 years but I’ve been looking for a ready-to-use function like that and noticed it had an issue :grin:

1 Like

i made this code in like 5 minutes, this might fix your problem
(just make sure this doesnt go over 4 years because leap years lol)

local seconds = 9738238 --set the seconds to whatever
local minutes = 0
local hours = 0
local days = 0
repeat seconds -= 86400 days += 1 until seconds < 86400
repeat seconds -= 3600 hours += 1 until seconds < 3600
repeat seconds -= 60 minutes += 1 until seconds < 60

local result = days..":"..hours..":"..minutes..":"..seconds