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

How would I extract hours from unix time?

Asked by 5 years ago

So I'm trying to just get the hour it is(hour of the day) for example in eastern it is

6:31 

as of the time posting this.

I'm trying to get the hours from unix time. Here is what I have done so far but i feel as it is innacurate and I am not sure where to go from here.

hourTime = ((tick() / 3600) % 12)

any help would be much appreciated :)

0
Local User#19524 175 — 5y
0
I can't decipher whether you are telling me to use a local variable or trying to tell me something else. 4d61736f6e 59 — 5y
0
I'd use os.time() to get the hours. bluestreakejjp 41 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

You're correct, (tick() / 3600) % 12 will return the correct hour of the day. The decimal places after that hour represent the percentage completed toward the next hour. In other words, the ratio of n minutes to 60. You could easily get the n minutes you're looking for by multiplying 60 by the preceding decimals...

local function getHourWithMinutes()
    local hour, alpha = math.modf(tick() / 3600 % 12)
    local mins = alpha * 60
    return string.format("%d:%d", hour, mins)
end

print(getHourWithMinutes())

8:17

Ad
Log in to vote
0
Answered by 5 years ago

You can get the current hour of the day by:

((tick()%86400)/3600)

An alternative is to use os.date:

local TIME = os.date(tick(), '!*t')
print(TIME.hour)
0
print(TIME.hour):1: bad argument #2 to 'date' (number expected, got string) DaCrazyDev 444 — 5y
0
local TIME = os.date("!*t") Thundermaker300 554 — 5y

Answer this question