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

How to use os.time() (or something else?) to get the hours, minutes, seconds time format?

Asked by
KDarren12 705 Donator Moderation Voter
5 years ago

First of all, I apologize sincerely for not writing a code. I would like to write a code, but I just want to print the hours, minutes, and seconds format in the console. I tried print(os.time()), expecting errors, and of course, I got errors. Can anyone just give me a redirect, or explain to me how to use this to get and print the hours, minutes, and seconds time format with os.time()? That would be very helpful.

1 answer

Log in to vote
0
Answered by 5 years ago

I would suggest using another function available in roblox os.date. This takes in a weird string ... either "!t" (corresponds to not using daylight savings time) or "t" (corresponds to using daylight savings time), and the unix timestamp provided by os.time() (this is a count of the number of seconds elapsed since some date in 1970, usually only machines parse this).

What you get back is a table containing components of the current date (including hours, minutes, seconds). For example.

mydatetable = os.date("*t", os.time())
for key, value in pairs(mydatetable) do
    print(key .. " , " .. tostring(value))
end

The above code generated the following output when I ran it a second ago (on an online lua interpreter unfortunately I don't have access to a roblox workstation atm).

min , 53
wday , 3
yday , 127
sec , 40
isdst , true
month , 5
year , 2019
day , 7
hour , 1

So to access hours, mins or seconds you would specifically use mydatetable['hour'], mydatetable['min'], mydatetable['sec'].

Cheers.

0
Also keep in mind the date hours, minutes, and seconds will be for the UTC timezone. Not the timezone you are likely in. User#18718 0 — 5y
0
Okay, thanks KDarren12 705 — 5y
Ad

Answer this question