Hi, I am making a daily quest system and I just want to know how to find out when the day will end in seconds. I am making a gui and it will say “Daily Quests Reset in (insert time until day end)”. Can someone please help.
EDIT: You have to explicit zero out the time components of the date.
A day is 86400 seconds. Find out the next day in seconds and then refeed the day, month and year into os.time to find out the os.time of the next day. Make sure you also zero out the time values so that the time its referencing is from 00:00 (12:00 AM).
The number of seconds left until the next day is os.time subtracted from that value.
local tomorrow = os.date("!*t", os.time() + 86400)
local tomorrowSeconds = os.time({
year = tomorrow.year, month = tomorrow.month, day = tomorrow.day,
hour = 0, min = 0, sec = 0 -- 12:00 AM
})
local secondsUntilTomorrow = tomorrowSeconds - os.time()
print(secondsUntilTomorrow)
Hi again. I thought it worked but I woke up today and it says 26:59:00 which is more than 24 hours so it is inaccurate/wrong. How to fix this?
This would be an issue with how many keys I specified in os.time for tomorrowZeroSeconds. Apparently you can’t just omit time keys otherwise it’ll fill in default values for 12:00 PM. I have updated the original code sample to include the time keys.
Specifically, the following need to also be present in the table given to os.time:
hour = 0, min = 0, sec = 0
Thank you, I know how to do it now!
Alternate Method
For anyone who finds this post, I’ve discovered a more “simplistic” method to accomplish the same result.
Example
local currentTime: number = workspace:GetServerTimeNow()
local timeSinceTheDayStarted: number = currentTime % 86400
local tomorrow: number = (currentTime + 86400) - timeSinceTheDayStarted
print(tomorrow - currentTime)
-- This will also get down to the decimal point if that's important to you at all