How can I make a time system?

How would I make a custom time system that updates the day every couple of seconds and eventually updates the month and year?

The time system should also show up the same for all players*

Can you describe a little more about what you mean and which parts you’re having trouble with?

Do you want to display a date on a GUI and advance the day it shows every X seconds?

Must it be synchronized across multiple servers?

Should it pause when the server shuts down or “continue” in the background?

I want to make a date system that advances every 2 seconds and for it to be shown on a gui
I want it to start advancing as soon as a server starts
It does not need to be synchronized across multiple servers

Are they representing real dates at an accelerated rate? Or just a single number that goes up?

Real dates at an accelerated rate

I would make a fake Unix timestamp that you increase at whatever rate you want

Then use the DateTime library to format it

Something like this

-- number of in-game seconds per real-life seconds
local TIME_SCALE = 60 * 60 * 12 -- e.g. 12 in-game hours passes every second

-- Date to start the server at
local initFakeDateMillis = DateTime.fromUniversalTime(
	2022, 11, 10, -- november 10, 2022
	12, 0, 0, 0 -- at 12:00 PM
).UnixTimestampMillis

local startedAtMillis = DateTime.now().UnixTimestampMillis

local function CurrentFakeTime(): DateTime
	local sinceStartMillis = DateTime.now().UnixTimestampMillis - startedAtMillis
	return DateTime.fromUnixTimestampMillis(initFakeDateMillis + sinceStartMillis * TIME_SCALE)
end

You can print out the date that function gives you using FormatUniversalTime.

Depending on what you’re using this for, you have some options for actually displaying it on a GUI. You could have clients just call this directly (in which case they won’t be synced with each other, or you can have the server send it to them over a remote.

If you want to have the server send the time to clients, you can get as fancy as you want. Some options for example:

  • send the time once a second and have the clients just display it when they get it, easy.
  • let clients request the time relatively infrequently, compute the round trip time so you can guestimate what time the server actually thinks it is, and have the client figure out what time it should be locally between sync requests
  • edit: I just learned about GetServerTimeNow which is probably the best way to do this. Id probably tweak the function to use that instead of DateTime.now() and call it on the client in the loop

Im fairly new to this so would you mind explaining how I can send the time from the server to the clients?