How to get correct UTC time from os.time() on client

I’m working on a date picker prompt for my game that allows the user to select a specific date and time. I’m having trouble converting the user defined dates into UTC time because for some reason os.time() does not return the correct hour on the client. UTC time is 6 hours ahead of my local time but for some reason os.time() is returning a value 2 hours ahead. I made a bug report here.

Os.time() does not return correct UTC time on client.

I’m working on an alternative in the mean time. I can’t seem to figure out how to get the time to offset properly based on the server time. For some reason the localDate variable ends up being 6 hours behind my local time.

--client
local timeOffset

Event.OnClientEvent:Connect(function(serverTime)
	timeOffset = os.time() - serverTime
	local UTC = os.time({year = Year, month = Month, day = Day, hour = Hour, min = Min ,sec = Sec}) + timeOffset
	local localDate = os.date("*t",UTC)
end)


--Server
Event:FireAllClients(os.time())

EDIT: Thank you AbiZinho DateTime was just what I was looking for

2 Likes

If you are working for a specific date and time if u parse os.time() a dictionary with keys; year, month, day, hour, min, sec it will multiply the values out to the unix epoch. My point being here is you can have your players input that dictionary then compare it to other inputs players have made.

I’m pretty sure os.time is relative to the client’s timezone, and so it’s different compared to the server’s, so it’s pretty much expected behaviour.

I also found this as reference, thought it might be useful:

Edit: Looks like I misunderstood os.time and mixed it up with tick, thanks @CoderHusk for the catch.

I think this may be a bug as well, not sure why that’s happening.

Have you looked into DateTime? You could use DateTime:ToUniversalTime to get the utc time on client.

As mentioned in your bug report; you can also use os.date("!*t") since that returns a UTC formatted date.

6 Likes

This is false os.time() is relative to the unix epoch.

I investigated this and apparently you can do this:

local currentTime = os.time()
local UTC = os.date("!*t", currentTime)
local localDate = os.date("*t", currentTime); localDate.isdst = false -- gotta fix this lol
local diffTime = os.difftime(UTC, localDate)

No needing to fire the event to the server.

Oogling around StackOverflow:


On top of that, I can’t observe the error on the os.time(). Might have been a fluke. I’ll check again later.


Further testing indicate no error on the hours, cannot reproduce. Perhaps there’s an error on the machine?

I went looking further and only found out that this also should work:

local currentLocalTime = os.time(os.date("*t"))
local currentUTCTime = os.time(os.date("!*t"))

I’m really confused on why your time is displaying incorrectly.

3 Likes

DateTime does the trick, thank you

1 Like