How would I go about converting a number into a clock time?

I need to convert a normal number into clock time for my game but I’m not sure how I would do this.

Clock time would be 90 converted to 1:30 like it would look like on a digital clock.

Thanks, ScriptKing.

ClockTime is a float which cycles through 0-24.

I know that, but I need to know how to convert 90 seconds into 1:30 (1 minute 30 seconds) with a function that can be used.

local lighting = game:GetService("Lighting")

local ctMinutes = lighting.ClockTime - math.floor(lighting.ClockTime)
local ctMinutes = math.round(60*ctMinutes)
local ctHours = math.floor(lighting.ClockTime)
print(string.format("%02i:%02i", ctHours, ctMinutes))
--output 14:00

Oh, that’s what you’re asking for.

2 Likes
local function secToMins(seconds)
	if seconds > 60 then
		local minutes = math.floor(seconds / 60)
		local remainingSeconds = seconds % 60
		return minutes..":"..remainingSeconds
	else
		return seconds
	end
end

print(secToMins(242))
--output 4:2 (4 minutes, 2 seconds)
4 Likes

Thank you! This is what I’ve wanted, I’ve never been good at math :grinning_face_with_smiling_eyes:

wouldn’t you want it to be 4:02 instead of 4:2?

1 Like

Well that would indicate 4 minutes and 20 seconds not 4 minutes and 2 seconds.

True, I think I know how to add on to it though to get that output. So it would be 4:02 instead of 4:2. I would just put:

If remainingSeconds < 10 then
RemainingSeconds = “0”..remainingSeconds
end

This would probably need some editing as I just came up with this on the top of my head to show.

1 Like
return string.format("%02i:%02i", minutes, remainingSeconds)

Single line of code change, basically the same as what I did in the other script I provided which wasn’t relevant.

It’ll display as “04:02” now.

1 Like

oh yea 4:02 :joy:

thanks for pointing that out

This will also work too however.

Zero is fine. “4:00” is correct.

If remainingSeconds < 10 then
RemainingSeconds = “0”..remainingSeconds
end

That’s the whole purpose of “if x less than 10 then”.

I would do “%01i:%02i” so that minutes doesn’t add an extra 0

Then you may as well just use %i instead.

1 Like

Sorry but could you display the whole code that I need? All the percents is confusing me :grinning_face_with_smiling_eyes:

Edit:

Nvm I figured it out! Thanks for the help!