Advice on improving time combinations

I’m making a time function that converts float numbers into time format, and if the minutes value is less than 10, it should be 05 (6:05:00 and not 6:5:0). And I made 18 lines of if statements just so it can work, is there a more efficient way to do this? Everything here works, I just want to know if it can be more concise

function MS.time(x,y) -- parameter 1 is for the time 0-24, parameter 2 is if the 12 or 24 hour system is used, if set to false, the script will use the 24 hours system.
	if y == nil or y == false then
		local hours = math.floor(x)
		local m = (x-hours)*60
		local minutes = math.floor(m+.5)
		local seconds = math.floor((m-minutes)*60+.5)
		if hours >= 0 and hours <= 24 then
			if hours < 10 and seconds < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. "0".. seconds
			elseif hours < 10 and seconds < 10 then
				return "0".. hours.. ":".. minutes.. ":".. "0".. seconds
			elseif hours < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. seconds
			elseif seconds < 10 and minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. "0".. seconds
			elseif seconds < 10 then
				return hours.. ":".. minutes.. ":".. "0".. seconds
			elseif minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. seconds
			elseif hours < 10 then
				return "0".. hours.. ":".. minutes.. ":".. seconds
			end
		end
	elseif y == true then
		local hours = math.floor(x)
		if hours > 12 and hours < 24 then
			hours = hours - 12
			local m = ((x-12)-hours)*60
			local minutes = math.floor(m+.5)
			local seconds = math.floor((m-minutes)*60+.5)
			if hours < 10 and seconds < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " PM"
			elseif hours < 10 and seconds < 10 then
				return "0".. hours.. ":".. minutes.. ":".. "0".. seconds.. " PM"
			elseif hours < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. seconds.. " PM"
			elseif seconds < 10 and minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " PM"
			elseif seconds < 10 then
				return hours.. ":".. minutes.. ":".. "0".. seconds.. " PM"
			elseif minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. seconds.. " PM"
			elseif hours < 10 then
				return "0".. hours.. ":".. minutes.. ":".. seconds.. " PM"
			end
		elseif hours == 12 and hours < 24 then
			local m = (x-hours)*60
			local minutes = math.floor(m+.5)
			local seconds = math.floor((m-minutes)*60+.5)
			if hours < 10 and seconds < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " PM"
			elseif hours < 10 and seconds < 10 then
				return "0".. hours.. ":".. minutes.. ":".. "0".. seconds.. " PM"
			elseif hours < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. seconds.. " PM"
			elseif seconds < 10 and minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " PM"
			elseif seconds < 10 then
				return hours.. ":".. minutes.. ":".. "0".. seconds.. " PM"
			elseif minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. seconds.. " PM"
			elseif hours < 10 then
				return "0".. hours.. ":".. minutes.. ":".. seconds.. " PM"
			end
		elseif hours == 24 then
			local m = ((x-12)-hours)*60
			local minutes = math.floor(m+.5)
			local seconds = math.floor((m-minutes)*60+.5)
			if hours < 10 and seconds < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " AM"
			elseif hours < 10 and seconds < 10 then
				return "0".. hours.. ":".. minutes.. ":".. "0".. seconds.. " AM"
			elseif hours < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. seconds.. " AM"
			elseif seconds < 10 and minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " AM"
			elseif seconds < 10 then
				return hours.. ":".. minutes.. ":".. "0".. seconds.. " AM"
			elseif minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. seconds.. " AM"
			elseif hours < 10 then
				return "0".. hours.. ":".. minutes.. ":".. seconds.. " AM"
			end
		elseif hours < 24 and hours > 0 then
			local m = (x-hours)*60
			local minutes = math.floor(m+.5)
			local seconds = math.floor((m-minutes)*60+.5)
			if hours < 10 and seconds < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " AM"
			elseif hours < 10 and seconds < 10 then
				return "0".. hours.. ":".. minutes.. ":".. "0".. seconds.. " AM"
			elseif hours < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. seconds.. " AM"
			elseif seconds < 10 and minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " AM"
			elseif seconds < 10 then
				return hours.. ":".. minutes.. ":".. "0".. seconds.. " AM"
			elseif minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. seconds.. " AM"
			elseif hours < 10 then
				return "0".. hours.. ":".. minutes.. ":".. seconds.. " AM"
			end
		elseif hours == 0 then
			hours = 12
			local m = ((x)-hours)*60
			local minutes = math.floor(m+.5)
			local seconds = math.floor((m-minutes)*60+.5)
			if hours < 10 and seconds < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " AM"
			elseif hours < 10 and seconds < 10 then
				return "0".. hours.. ":".. minutes.. ":".. "0".. seconds.. " AM"
			elseif hours < 10 and minutes < 10 then
				return "0".. hours.. ":".. "0".. minutes.. ":".. seconds.. " AM"
			elseif seconds < 10 and minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. "0".. seconds.. " AM"
			elseif seconds < 10 then
				return hours.. ":".. minutes.. ":".. "0".. seconds.. " AM"
			elseif minutes < 10 then
				return hours.. ":".. "0".. minutes.. ":".. seconds.. " AM"
			elseif hours < 10 then
				return "0".. hours.. ":".. minutes.. ":".. seconds.. " AM"
			end
		end
	end
end

This might be simpler for what you need:

local function toHMS(s, twelveHourFormat) -- Takes seconds as first param and bool as second
	if not twelveHourFormat then
		return string.format("%02i:%02i:%02i", s/3600, s/60%60, s%60)

	else
		local hours = s/3600
		local over12 = hours > 12
		
		hours = (over12 and hours - 12) or hours
		
		return string.format("%02i:%02i:%02i", hours, s/60%60, s%60) .. (over12 and " PM" or " AM")
	end
end

print(toHMS(50000, true))
print(toHMS(50000, false))

It isnt determining it by seconds but hours, i tried tweeking the code by removing the division, but ended up with a weird output

local function toHMS(s, twelveHourFormat)
	if not twelveHourFormat then
		return string.format("%02i:%02i:%02i", s, s/60%60, s%60)

	else
		local hours = s
		local over12 = hours > 12

		hours = (over12 and hours - 12) or hours

		return string.format("%02i:%02i:%02i", hours, s/60%60, s%60) .. (over12 and " PM" or " AM")
	end
end

print(toHMS(1.5, true)) --  01:00:01 AM
print(toHMS(11.25, false)) -- 11:00:11

Here is what im trying to do
image
clock time is the hours, and the decimal is the minutes and seconds, time of day puts it into 24 hour format

Ah ok, I came up with this function to convert hours to time format:

local function toHMSFromHours(hours, twelveHourFormat) -- hours as first param and bool as second (twelveHourFormat)
	local h, m = math.modf(hours)
		
	local over12
		
	if twelveHourFormat then
		over12 = h > 12
		h = (over12 and h - 12) or h
	end
		
	m *= 60

	local s

	m, s = math.modf(m)
	s *= 60
	s = math.round(s)

	if s >= 60 then -- needed for a few edge-cases, not sure exactly why (e.g. try X.1 with and without this part)
		s -= 60
		m += 1
	end

	return string.format("%02i:%02i:%02i", h, m, s) .. (twelveHourFormat and (over12 and " PM" or " AM") or "")
end

I realised that this function does have some bugs where the time isn’t displayed correctly.
I’ve come up with a new version now, hopefully this will be a lot better: