Os.time week countdown

I have a leaderboard that shows weekly stats. Assuming that I am getting the current week with os.time, is there a way I could have a “countdown” until a new week begins so players would know when the board will reset. (Of course, then it would start to countdown again at the start of a new week) I know how to display it onto a UI, but how would I take care of the scripting end of things.


Sorry for being vague, but have you looked into os.date? It could really be useful over os.time.

I’m using this to determine the week,

local length = 60 * 60 * 24 * 7
local number = math.floor(os.time()/length)
local key = “Week” … tostring(number)

I find it useful for my needs as far as the datastore and leaderboard are concerned, I’m just looking for a way to physically countdown every week through a UI. Is this what you recommend os.date for?

So you want a timer that says how much time until the leaderboard resets? You could get the unix time from random a Monday at 12:00 am and then add 6060247X, with X being how many weeks have passed since that initial date. Then once you have that time you can just do (endOfWeek - os.time) for your countdown.

If you just need code to format the time to Days:Hours:Minutes:Seconds you can use the following code:

local function FormatCountdownDHMS(seconds)
	return string.format("%d:%02d:%02d:%02d", seconds/86400%7, seconds/3600%24, seconds/60%60, seconds%60)

local CurrentWeek = os.time()
local NextWeek = CurrentWeek + 604800

while true do
	local SecondsTillNextWeek = NextWeek - os.time()

Every time a server starts, the countdown just starts from the beginning as if it’s counting down a week from when the server was created.

The leaderboard is in working order, essentially, I’m just trying to countdown every week continuously according to UTC. From Sunday at midnight until the next Sunday (or whatever day os.time declares the start of a week), the countdown will do it based on the amount of time until a new week starts.

I assume this would be the same for my daily countdown, which continuously countdowns every day according to os.time. @DJX_D’s code works perfectly, except that it restarts when the server is created. How would I avoid this?

To do it like that you have to do something like @Usering said and pick and arbitrary date to calculate from. I haven’t really tested this, but I picked the Unix/epoch time of 1/17/2038 which is the last Sunday it will be valid and hopefully far enough in the future you shouldn’t have to worry. This isn’t really best practice, I would try to sync it with the leaderboard code in case it gets out of sync somehow.

local ArbWeekStart = 2147299200 --Unix time of 1/17/2038 0:00 AM, last valid Sunday value

while true do
	local SecondsTillNextWeek = ArbWeekStart - os.time()

Ah, I see now. I modified your original code sample and it works now. I was checking the countdown based on Monday which I should have check from Sunday at midnight instead. Both do work however, thanks for the help.

This should format and show the remaining time to complete any week, starting from Monday, on UTC:

local CountdownUntilNextWeek do
	local s = "%s%02d:%02d:%02d"
	function CountdownUntilNextWeek(t)
		t = os.date("!*t", t)
		t.wday, t.hour, t.min, t.sec = (8 - t.wday) % 7, 23 - t.hour, 59 - t.min, 59 - t.sec
		t.wday = t.wday > 0 and t.wday .. (t.wday == 1 and " Day, " or " Days, ") or ""
		return s:format(t.wday, t.hour, t.min, t.sec)

print(CountdownUntilNextWeek()) -- countdown: 5 Days, 07:01:34

use os.date, os.time is unnecessary

you should use 59-min and 59-sec and etc because your example at 0 would display 60 and at 59 would display 1, clocks are typically 0-59

It’s fixed now, thank you.