Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago
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

Log in to vote
1
Answered by
ozzyDrive 670 Moderation Voter
5 years ago
Edited 5 years ago

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()))
0
Doesn't work ixUltraz 28 — 5y
0
Updated the answer. I tried the original line with Lua 5.3. ozzyDrive 670 — 5y
0
Ok I'll try ixUltraz 28 — 5y
0
Thanks it works now. ixUltraz 28 — 5y
Ad

Answer this question