Function to get same time across server/clients

Is there a function that returns a time that’s the same across the server and all clients regardless of their timezone?

1 Like

Use the DateTime.Now() function. I couldn’t find the API on the dev wiki but here’s the update post that came out a while back: Release Notes for 409 Some users posted the syntax, functions and whatnot.

Hope this helped :smiley:

1 Like

Are you looking for os.date/time ? using that you can use the !*t format to get UTC time:

os.date("!*t")

DateTime should work too, they are pretty much the same or provide the same stuff

You forgot to put that in quotation marks

os.date("!*t")
1 Like

Just like os.time(), os.date() seems to be 13-14 seconds behind on the client for people on GMT when the server is NA West. I’m not sure why e.e

hmm… it shouldn’t be as far as I know… i’m not sure why it would have this behavior. Does using any part of DateTime do this aswell?

I don’t think DateTime is available yet. DateTime.now() returned nil. To fix this issue, I just switched to a “time” counter controlled by the server (adds 1 to an IntValue every second)

1 Like

It is inconsistent because it returns the state of the internal clock of the machine the code is ran on at that time and since that differs between each machine by a bit you will always get an inconsistent and different results each time you run the code on different machines.

The best you can do to synchronize those times is to roughly calculate the difference of os.time() between the client and server and simply take that difference into account when running your code.

For example:

--Client
local giveOsTimeRemote = someRemoteFunction
local lastClientTime = os.time()
local serverTime = giveOsTimeRemote:InvokeServer()
local currentClientTime = os.time()
local roughDifference = (lastClientTime+currentClientTime)/2 - serverTime


print("The code on the Server ran roughly "..(os.time()-serverTime-roughDifference).." seconds ago")


--Server
local giveOsTimeRemote = someRemoteFunction

local function onRequestingServerTime()
return os.time()
end

giveOsTimeRemote.onServerInvoke = onRequestingServerTime

3 Likes