I am using os.date to display the current time as a digital clock, but I would rather it showed the 12 hour times without the leading zero, ie 5:08, 7:15, 3:02, instead of 05:08, 07:17, or 03:02. Is there a way to do this in lua?
Thanks.
I am using os.date to display the current time as a digital clock, but I would rather it showed the 12 hour times without the leading zero, ie 5:08, 7:15, 3:02, instead of 05:08, 07:17, or 03:02. Is there a way to do this in lua?
Thanks.
local
date =
os.date
(
"*t"
)
-- Returns table of date & time values in local time
print
( date.hour, date.min )
The method in the document will not have 0
Thanks. The leading zero is gone, which is great, however, this method returns 24 hour time (aka military time), while I need to return 12 hour time. I’m not lua savvy enough to know if I’m missing something here.
You can write a judgment.
Since I can’t test it now, I’m not sure whether the output value is a number. If it is, you can remove tonumber().
local TrueHour = tonumber(date.hour)
if TrueHour >= 13 then -- 12 Military = 12 PM
TrueHour = TrueHour - 12
end
I tried using this and it seemed to work:
if date.hour > 12 then
date.hour = date.hour - 12
Thanks again.
I had read that (thanks Google), but it wasn’t clicking in my head. Still isn’t TBH.
Okay, here’s what I pieced together thanks to the tips from vb66r55, including info in the post they deleted. It works, so thanks much!
local clockface
local date = os.date ( “*t” ) – Returns table of date & time values in local time
local Hour = date.hour
local Min = os.date( “:%M” ) – date.min would return minutes, but lose the leading 0 on 1-9
if date.hour > 12 then
Hour = date.hour - 12
end
clockface = “”…Hour…""…Min…""
print(tonumber(os.date("%I"))..os.date(":%M")..os.date(":%S"))