How do I check if a certain day has been passed?

So I’ve got a code system in which players can enter and receive prizes. However, it is a huge pain when I had to manually remove the code from the module script, the data is set up like this:
image
I would then do a simple if codeData[inputCode] then to then validate the code.

But is there a way to set up this data so that it checks if the additional data of [“Expires”] makes the code valid, and if it exceeds that date then it will not continue the rest of the code that awards the prize.

4 Likes

Hi there,

There is certainly a way to check the date using os.date.

I used this:

local date = os.date(“*t”, os.time()) print(date.hour)

“*t” means local time for users. That depends on the time zone. “!*t” is UTC, or absolute time for the whole world. You can choose whichever one is more suitable for your case.

os.time() is the number of seconds from January 1, 1970 on 12:00 am. You input that as the second parameter. This then calculates what year, month, day, and etc. it is currently.

So the above currently returns:

13

13 is the hour of day (it is 1:00 pm for me, so it is correct!).
For the current situation, the code would be:

local code = {
	
	["Expire"] = {Year = 2019, Month = 12, Day = 23, Hour = 11, Minute = 0, Second = 0}
	
}

local date = os.date("*t", os.time())
local expire = code.Expire

if date.year >= expire.Year and date.month >= expire.Month and date.day >= expire.Day and date.hour >= expire.Hour and date.min >= expire.Minute and date.sec >= expire.Second then
	
	print("expired!")
	
else
	
	print("still got time!")
	
end

So, currently, this prints:

still got time!

because right now it is not December. You can use a system like this, replacing the print with your code, and your expiring system would work!

Hope that helps!

11 Likes

Using the == operator would mean that it’d have to directly be that, consider using the >= operator.
Also in os.date(), if a time is not given then it defaults it to os.time().

5 Likes

Thank you this is perfect! I will use this effectively

1 Like