Os.time and os.date confusion

local UTCTime = os.time()
local UTCDate = os.date("!*t")

print(UTCDate.hour) --> 17
print(os.date("%H", UTCTime)) -- not the same as UTCDate.hour

I am very confused on this os library.

According to the library, os.date 2nd argument is defaulted to the value of os.time, which is UTC time.
Furthermore, os.date("!*t") should also return the same value as os.time when converted to seconds, which is still UTC time.

Now, when I printed them, UTCDate.hour returns a number (example: 17)
For os.date("%H", UTCTime), I would assume it would also return the same (example: 17), but what it outputted is a different number.

Can someone explain this for me? I am very confused.

Removing the ! would fix this

local UTCTime = os.time()
local UTCDate = os.date("*t")

print(os.date("%H", UTCTime))

According to Lua os.date() - What's the difference between *t and !*t - Stack Overflow

local t = os.date( "!%a %b %d, %H:%M")
print(t) -- Thu Oct 25, 04:01 (current UTC)

local t = os.date( "%a %b %d, %H:%M")
print(t) -- Thu Oct 25, 12:01

The problem is that os.date() with os.time() returns local time, see screenshot, this is so because os.time() uses local time, but since we play on the server, and we check the code in the studio, where there may be another time - there may be discrepancies
In studio
In Game
You can also try removing the !, as @RickAstll3y said, it will show you what’s with os.time(), what’s with os.date() is the local time, there shouldn’t be any problems in the game, but if you want to use it in local scripts, then I recommend either using os.time() or os.date("*t") so that players see the time in their time zone

Why does Roblox Studio return the local time using os.time() when in the documentation:

Returns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00), under current UTC time

After checking it in a live server, os.time() does return the UTC time. But in studio, it returns the local time.

os.date() converts it to local

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.