Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I extract hours from unix time?

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 :)

3 comments

0

Local

0

I can't decipher whether you are telling me to use a local variable or trying to tell me something else.

0

I'd use os.time() to get the hours.

2 answers

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

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)

2 comments

0

print(TIME.hour):1: bad argument #2 to 'date' (number expected, got string)

0

local TIME = os.date("!*t")

This discussion is preserved as it was. New answers, comments, and votes are closed.