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.
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()))