Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How to check if a date is greater than another date in roblox lua?

Asked by 5 years ago

Hello everyone. So I'm making a ban gui, that uses date as a source to define how long players are banned for. I have a script for it, that supposedly checks if a date is higher than another. And if so the player gets kicked for being banned because the date is still not right. But it doesn't work. Here's the code:

Let's say DayD, MonthD and YearD are respectively 1 5 2019 as today's date

And let's say DayP, MonthP and YearP are respectively 16 8 2019 as the date the player is banned until

How would I correct this code to work properly?

if YearD <= YearP then --Still banned
        game.Players:FindFirstChild(tostring(PlayerWhoJoined)):Kick("You are banned.")
    else --Year is good, check month!
        if MonthD <= MonthP then --Still banned 
            game.Players:FindFirstChild(tostring(PlayerWhoJoined)):Kick("You are banned.")
        else --Month is good, check day!
            if DayD <= DayP then --Still banned
                game.Players:FindFirstChild(tostring(PlayerWhoJoined)):Kick("You are banned.")
            else--Player is good, and will get unbanned.
                      --Unban player code here

**Thank you very much to those who try to help! **

1
What an amazing question !!!!!! UPVOTE THIS PLS (I am writing an answer currently) User#24403 69 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago

os.date(str, time)

The os.date function returns a dictionary that has all the information of the date at given time. str can either be *t or !*t. The former will get the local timezone where the latter gets the UTC time. The last argument defaults to the current number that os.time returns.

Here's a list of the keys and values of the dictionary returned:

Key Value
sec Current seconds of the hour.
min Current minute of the hour.
hour Current hour of the day.
day Current day of the month.
wday Current weekday. (Monday, Tuesday, ect).
yday How many days we are into the year. For example, as of February 9, 2019, we are 40 days into the year 2019.
month Current month of the year.
year Current year.
isdst Boolean that is true if there is daylight savings time. False otherwise.

In this example I will use !*t to get UTC time

local info = os.date("!*t")

if info.year < yourYear then
    -- # ...
end

if info.month < yourMonth then
    -- # ...
end

if info.day < yourDay then
    -- # ...
end

And you'd just do whatever you need to do.

0
I managed to implement your information into my code and now it is working just as expected. You have my gratitude. wilsonsilva007 373 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You would not store the date text. You would store the time plus the number of days the ban is in seconds.

Example

local days = 60 * 60 * 24  -- the number of seconds in a day

local function ban(numberOfDays)
    return os.time() + (days  * numberOfDays)
end

You can then compare the current time is larger than the ban time.

Hope this helps

0
Hm, I don't really get it. wilsonsilva007 373 — 5y

Answer this question