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

How do I make a TextLabel display the current UTC time with AM/PM?

while true do
	wait()
	 local hour = os.date("!*t")["hour"]
 	 local min = os.date("!*t")["min"]
  	 local sec = os.date("!*t")["sec"]
	
	script.Parent.Text = hour..":"..min..":"..sec
end

So far the script works but I need it to display AM and PM.

1 answer

✓ Accepted answer

For Lua 5.1, you can perform a simple conditional and a mathematical operation to calculate the 12-hour time and add the suffix:

local function formatTime(t)
	local dateTable = os.date("!*t", t)
	local hour, min, sec = dateTable.hour, dateTable.min, dateTable.sec
	local period = "AM"
		
	if hour >= 12 then
		period = "PM"
		hour = hour ~= 12 and hour - 12 or hour
	end
	
	return string.format("%.2d:%.2d:%.2d %s", hour, min, sec, period)
end

while true do
	print(formatTime())
	wait()
end

For newer Lua versions:

os.date accepts a format string. All the tags can be found in PIL. There are options for the 12 hour clock.

print(os.date("%I:%m:%S %p", os.time()))

4 comments

0

Doesn't work

0

Updated the answer. I tried the original line with Lua 5.3.

0

Ok I'll try

0

Thanks it works now.

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