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

But then it will display 242 seconds

242 was just an example, you don’t need that.

Ik but I am talking about using os.time and getting a bigger number that I later use to get the hrs mins and secs

Here, use this function.

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

local seconds = 283
toDHMS(seconds)
3 Likes

https://developer.roblox.com/en-us/api-reference/lua-docs/os

Use the os.difftime() function.

1 Like
local function SecondsToString(seconds)
    local days = math.floor(seconds / 86400)
    local hours = math.floor(seconds % 86400 / 3600)
    local minutes = math.floor(seconds % 3600 / 60)
    seconds = math.floor(seconds % 60)
    return string.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
end
3 Likes

How about seconds to HMS? Hours, minutes, seconds

For HMS:

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

local seconds = 283
toHMS(seconds)

@Philipceo90

Use this instead:


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

local seconds = 283
toHMS(seconds) — toDHMS -> toHMS

That’s the same thing… it’s the same exact script.

No, you said toDHMS() instead of toHMS(), and then changed it right when I corrected you

I must have edited right when you posted that

Probably so.

filler wordhereqjeriwnq

How would I add months years also where do u find all this info

A long time ago when I needed it I searched the DevForum and found that. It’s just some basic string manipulation that you can learn off the DevHub.

You can use os.date() (you could actually use the for h:m:s and d:h:m:s aswell)

local curDateInSeconds = os.time()
local seconds = 35869214
local formatted = os.date("!%Y:%m:%H:%M:%S", (curDateInSeconds + seconds))

print(formatted) --2023:09:19:03:21
1 Like

could i use string.format tho cause i use tick() as os.time doesnt work properly for some reason

They’re not the same thing. Format only allows you to insert variables into a string. You just need to keep dividing or multiplying depending on which direction you need to go.

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