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 6 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?

01if YearD <= YearP then --Still banned
02        game.Players:FindFirstChild(tostring(PlayerWhoJoined)):Kick("You are banned.")
03    else --Year is good, check month!
04        if MonthD <= MonthP then --Still banned
05            game.Players:FindFirstChild(tostring(PlayerWhoJoined)):Kick("You are banned.")
06        else --Month is good, check day!
07            if DayD <= DayP then --Still banned
08                game.Players:FindFirstChild(tostring(PlayerWhoJoined)):Kick("You are banned.")
09            else--Player is good, and will get unbanned.
10                      --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 — 6y

2 answers

Log in to vote
2
Answered by 6 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

01local info = os.date("!*t")
02 
03if info.year < yourYear then
04    -- # ...
05end
06 
07if info.month < yourMonth then
08    -- # ...
09end
10 
11if info.day < yourDay then
12    -- # ...
13end

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 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 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

1local days = 60 * 60 * 24  -- the number of seconds in a day
2 
3local function ban(numberOfDays)
4    return os.time() + (days  * numberOfDays)
5end

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 — 6y

Answer this question