Ad
Log in to vote
1

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

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

Log in to vote
4
Answered by 6 years ago
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.

1
If you want the number to show before the actual "AM" use concatenation (Time.." AM") RaverKiller 668 — 6y
0
Ohh! I should've known that! Thank you!! Devotional 210 — 6y
1
No problem :) RaverKiller 668 — 6y
Ad

Answer this question