How to detect if it is past a certain date?

I found a script that detects the current day. But I want to know how to detect if it is past a specific date, but I don’t know how I would do it.

Here is the script:

	local months = {"January","February","March","April","May","June","July","August","September","October","November","December"}
	daysinmonth = {31,28,31,30,31,30,31,31,30,31,30,31}

	local function getDate()
		time = os.time()
		local year = math.floor(time/60/60/24/365.25+1970)
		local day = math.ceil(time/60/60/24%365.25)
		local month
		for i=1, #daysinmonth do
			if day > daysinmonth[i] then
				day = day - daysinmonth[i]
			else
				month = i
				break
			end
		end
	
		return month, day, year
	end

	local month, day, year = getDate()
	local Date = month.."/"..day.."/"..year
	

Ignore the bad indenting

Not to question you by any means, but you are aware of os.date() right?


https://gyazo.com/62677df251c886ac7616cfa641d79e79

3 Likes
local function getMonthIndex(month)
    for i, v in pairs(months) do
        if v == month then
            return i;
        end
    end
    warn("Couldn't find month")
end

local function checkDate(expectedDate)
    local m, d, y = getDate()
    return y >= expectedDate["year"] and getMonthIndex(m) >= getMonthIndex(expectedDate["month"]) and d >= expectedDate["day"]
end
checkDate({month = "January", day = 12, year = 2019})
3 Likes

So the script has a function that will detect if it’s past a certain date?

if all three requirements are met in the return statement it will return true, if not it will return false; simply just checks to see if the date values are greater than the values of the date you want to check against.

2 Likes

Could I replace it with an or instead of and?

You can convert os.date into os.time and compare that with your value instead of doing all of that messy logic.

Interestingly, The DevHub doesn’t state how to use it fully, Programming in Lua : 22.1

3 Likes

if you replace it with an or, then it wouldn’t need to meet all the requirements to be passed that date;
say the current day is 09/14/2019, and you want to check against 09/13/2020; it would pass solely because 14 is greater than 13. However as @RuizuKun_Dev stated above you could do that as well.

1 Like

But what if the date is this: 9/14/2019, and we wanted to compare it against a date in the same month or year? Would it pass?

if flipped to an or, then only 1 value needs to be >= to the expected date equivalent value and it will pass yes.

1 Like