Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make this print an output with AM/PM?

function sTime()
	t = tick()
	local sec = math.floor((t%60))
	local min = math.floor((t/60)%60)
	local hour =  math.floor(math.fmod((t/3600),24))
	print(hour)
	if not (sec<10) then sec = math.floor(sec) else sec = "0"..math.floor(sec) end
	ssec = sec
	mmin = tostring((min < 10 and "0" or "") .. min)
	tag = (Type == 1 and (hour < 12 and "AM" or "PM") or "")
	mhour = math.fmod(hour,12)--[[((Type == 1 and hour < 10 and "0" or "") .. tostring(Type == 0 and (hour == 0 and 12 or hour > 12 and (hour-12) or hour) or hour))	-- Ternary operators FTW. Helps get rid of 'if then' stuff. Actually learned this off of Java programming:	z = (x == y ? "Yes" : "No")]]
	local c,s = ":",(Type == 1 and " " or "")	-- Colon, (space if 12 hr clock)
	Time = (mhour .. c .. mmin .. (ssec and c .. sec or "") .. s .. tag)
	return Time
end

Right now, it's printing something like 11:11:12, and it doesn't show AM or PM. This isn't something I made, I'm just using it to get the time, and the coding is a bit hard for me to understand it completely. So does anyone know how I could change this to make it show up with an AM or PM tag?

Thank you!

1 answer

✓ Accepted answer
function sTime()
    t = tick()
    local sec = math.floor((t%60))
    local min = math.floor((t/60)%60)
    local hour =  math.floor(math.fmod((t/3600),24))
    print(hour)
    if not (sec<10) then sec = math.floor(sec) else sec = "0"..math.floor(sec) end
    ssec = sec
    mmin = tostring((min < 10 and "0" or "") .. min)
    tag = (Type == 1 and (hour < 12 and "AM" or "PM") or "")
    mhour = math.fmod(hour,12)--[[((Type == 1 and hour < 10 and "0" or "") .. tostring(Type == 0 and (hour == 0 and 12 or hour > 12 and (hour-12) or hour) or hour))    -- Ternary operators FTW. Helps get rid of 'if then' stuff. Actually learned this off of Java programming:  z = (x == y ? "Yes" : "No")]]
    local c,s = ":",(Type == 1 and " " or "")   -- Colon, (space if 12 hr clock)
    Time = (mhour .. c .. mmin .. (ssec and c .. sec or "") .. s .. tag)
	if hour < 12 then
		return "AM"
	else
		return "PM"
	end
end

print(sTime())

There, it is checking the hour which is already an int.

3 comments

1

If you want the number to show before the actual "AM" use concatenation (Time.." AM")

0

Ohh! I should've known that! Thank you!!

1

No problem :)

This discussion is preserved as it was. New answers, comments, and votes are closed.