Issues learning time os

Hello anyone know whats wrong in this code? (im learning)

local date = os.time()
local day2year = 365.242 -- days in a year
local sec2hour = 60 * 60 -- seconds in an hour
local sec2day = sec2hour * 24 -- seconds in a day
local sec2year = sec2day * day2year -- seconds in a year
-- year
print(date // sec2year + 1970) --> 2021.0
-- hour (in UTC)
print(date % sec2day // sec2hour)
-- minutes
print(date % sec2hour // 60)
--seconds
print(date % 60)

I use a calculator using the same formulas and it worked fine..

Replace the double slashes to a single slash, as such: /

Actually, disregard my previous response… can you explain what exactly the issues seem to be?

hum, nĂŁo sou programador, perdĂŁo.

If you want the individual components of time this is built in already:

local now = os.time()
local asATable = os.date("*t", now)
print(asATable.year)
print(asATable.hour)
print(asATable.min)
print(asATable.sec)

If you just want a string like “The time is 10:48 PM on April 18, 2022” there are even easier ways, which I’ll explain if you want.

2 Likes

Thank you very much! yes, i would like to know :smiley:

Passing "*t" to os.date means “break this into its components as a table”. Technically you don’t need to give it the second argument, it defaults to “now”.

Passing anything other than "*t" means “look for specific substrings in this and replace them with the associated components”. For example:

local now = os.time()

local formatString = "It is %A, %x at %I:%M:%S %p"

print(os.date(formatString, now))

Prints out

It is Tuesday, 04/19/22 at 08:54 AM

The full list of options is here: os | Documentation - Roblox Creator Hub

2 Likes