✓ 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
Doesn't work
Updated the answer. I tried the original line with Lua 5.3.
Ok I'll try
Thanks it works now.