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

Can someone possibly explain os.date() to me?

Asked by
iRexBot 147
6 years ago

I have looked at os.date() on the roblox wiki but the roblox wiki does not have a good explanation for me. Could anyone explain it to me? It's suppost to get the date and time.

1 answer

Log in to vote
1
Answered by
fredfishy 833 Moderation Voter
6 years ago

Computers generally use a thing called UNIX time, where we store time as a number of seconds after 1st January 1970 00:00:00 (GMT/UTC). This is known as the epoch time.

To get this epoch time, we use os.time(). The other function you might be familiar with is tick(). The difference is that the first gets the actual UTC time, and the second gets whatever time the user's computer THINKS it is.

Anyway, "seconds since epoch" is a bit of a faff to work with. Generally, it's a very, very, very, very bad idea to attempt to code your own date-time system. No matter how much you might think you know what you're doing, you don't. So what you do is you just use somebody else's.

That's where os.datetime(...) comes in. We feed it two things - a "format string" and UNIX time. The format string we give it is either *t for client time, or !*t for UTC. For UNIX time, we use os.time().

So, what we want is probably something like os.datetime("!*t", os.time()).

That gives back a dictionary, which you can use the table in the wiki to decode.

dt = os.datetime("!*t", os.time())

-- I think this should do what you're after. 
print("Day: " + tostring(dt["day"]))
print("Month: " + tostring(dt["month"]))
print("Year: " + tostring(dt["year"]))
Ad

Answer this question