How to get the last day of the month?

Hello, I’m trying to make my own banlist script, I’m making everything of my own so far… but I have a problem…

I can’t seems to find how to get the last day of the month (cf: if the day end a 30 or a 31)

I tryed to search on forum and google something to determine the last day of a specific month, but can’t seems to find anything…

Here my script so far:

function module:GetDateFromDuration(duration, method) --- convert a ban duration into a date format
	
	local Method = "day"
	
	if method then
		Method = method
	end
	
	
	local date = {}
	date.year = module:GetTimeOS("year")
	date.month = module:GetTimeOS("month")
	date.day = module:GetTimeOS("day")
	date.hour = module:GetTimeOS("hour")
	date.min = module:GetTimeOS("min")
	date.sec = module:GetTimeOS("sec")
	
	
	for count = 0, duration, 1 do
		if method == "sec" then
			date.sec = date.sec + 1
		elseif method == "min" then
			date.min = date.min + 1
		elseif method == "hour" then
			date.hour = date.hour + 1
		elseif method == "day" then
			date.day = date.day + 1
		elseif method == "month" then
			date.month = date.month + 1
		elseif method == "year" then
			date.year = date.year + 1
		end
		
		if date.sec >= 60 then
			date.sec = date.sec - 60
			date.min = date.min + 1
		end
		
		if date.min >= 60 then
			date.min = date.min - 60
			date.hour = date.hour + 1
		end
		
		if date.hour >= 24 then
			date.hour = date.hour - 24
			date.day = date.day + 1
		end
		
		if date.day >= 31 then --- PROBLEM IS HERE! I have no idea how to check if month end a 30 or a 31?
			date.day = 1
			date.month = date.month + 1
		end
		
		if date.month > 12 then
			date.month = 1
			date.year = date.year + 1
		end
	end
	
end

Just make a quick table and fill it with all the months. Like
daysInMonths = {31, 28, 31, etc}

And then check
if date.day > daysInMonth[date.month]

2 Likes

Seems like the only solution here…

I am not exactly sure what you are trying to achieve but i’ll tell you the simplest and most effective way to do a ban using unix epoch time.

Let’s say we want to ban someone for 24hours, we’ll do something like this:

local function banPlayer(player: Player, howLongInSeconds: number)
    local playerData = ... -- I use ProfileService to save/get player's data
                           -- but you may use whatever you want

    -- This gives us how many seconds passed since 1970, aka epoch
    local now = workspace:GetServerTimeNow()

    -- Now in the data store we save the ban period
    -- so if you want to ban them for 24h,
    -- `howLongInSeconds` would be 24 * 3600 (hours to seconds conversion)
    playerData.Ban = now + howLongInSeconds
    player:Kick()
end

Now when the player joins, here’s how we know if we should kick them or not:

local function toKickPlayer(player: Player)
    local playerData = ...

    -- All we do is check if our time now is smaller than
    -- the time we calculated beforehand
    -- because that tells us we havent reached the future where they are unbanned yet
    local now = workspace:GetServerTimeNow()
    if now < playerData.Ban then
        player:Kick()       
    end
end

But if that’s not what you want, os.date("*t", now) returns a dictionary with all relevant dates

Hello, I don’t want to store the ban on a PlayerProfile, it will be on a global table that I already made.

The main purpose of the script that I showed above is to get a date to display on the “banlist” that the moderators can see via the moderator pannel.

Basically my banlist looks like this:

Banlist[PlayerID] = {
		["Reason"] = reason;
		["BanStart"] = module:GetTimeOS("date");
		["BanDuration"] = duration;
		["BanEnd"] = ExpireDate;
	};

The function I posted on the initial post is to get the “ExpireDate” the value.

1 Like

Thank you, this is exactly what I was looking for!

It doesnt matter where you store it, it just matters how you store it, if you store the epoch time then you can get all those three BanStar, BanDuration, BanEnd values very easily

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.