Compare two dates

Hi
I have code’s expiration date set to

os.date("%x", os.time({year = 2022, month = 2, day = 25}))

and I want a script to get the current date and compare it to the code’s expiration date and if current date is before or equal to code’s expiration date then it passes else it doesn’t.
I tried this:

    local currentDate = os.date("!*t")
	local codeDate = code.expirationDate
	if currentDate.year <= codeDate.year and currentDate.month <= codeDate.month and currentDate.day <= codeDate.day then
        -- rest of the code
    end

But it doesn’t seem to work, also tried (before that) doing:

os.time() <= code.expirationDate

But no luck with that too.

os.time() returns a bunch of numbers representing seconds, os.date("%x", os.time()) is formatting those numbers into MM/DD/YY. The comparison of say 12345678 == "02/25/22" isn’t producing the results you want; you will need to compare os.time() to the unformatted return of os.time({year = ...})

1 Like

You could try to get the numbers you used in expirationdate by converting the string you have 02/25/22 to numbers and just using os.time without making it into a string, then you can compare that number to currentdate.
local str = string.split(expirationdate, "/")
local result = os.time({year = tonumber("20"..str[3]), month = tonumber(str[1]), day = tonumber(str[2])})

Isn’t os.time tied to the time that is set on your computer?

1 Like

That would be tick(), os.time() will return time since unix epoch in UTC

I just read in the wiki and it says that they both return time since UNIX epoch.

Right, but time will return in UTC, tick will return in local timezone. Two people calling tick() in different timezones will get a different number of seconds since the unix epoch, as odd as that sounds.

1 Like

But this post says opposite🧐