How to get the remaining time until next month?

I’d like to get the remaining time until next month so that it can be displayed in a text, but I’m not sure how.

The time should be look like this “20 days:12 hours:2 minutes:2 seconds” left until next month.

1 Like
local countdownText = script.Parent
local str
local day = os.time({year = 2022, month = 8, day = 0, hour = 0, min = 0, sec = 0})
while wait() do
    local secondsBetween = os.difftime(day, os.time())
    local secs = secondsBetween % 60
    local mins = math.floor(secondsBetween % (60*60) / 60)
    local hours = math.floor(secondsBetween % (60*60*24) / (60*60))
    local days = math.floor(secondsBetween % (60*60*24*30) / (60*60*24))
    if days > 0 then
        str = days .. "d:"
        if hours <= 9 then
            str = str .. "0"
        end
        str = str .. hours .. "h:"
    else
        str = hours .. "h:"
    end
    if days > 0 or hours > 0 then
        if mins <= 9 then
            str = str .. "0"
        end
        str = str .. mins .. "m:"
    elseif mins > 0 then
        str = mins .. "m:"
    end
    if days > 0 or hours > 0 or mins > 0 then
        if secs <= 9 then
            str = str .. "0"
        end
        str = str .. secs .. "s"
    else
        str = secs .. "s"
    end
    countdownText.Text = str
    if secondsBetween <= 0 then break end
end

--> Gui would be like 20d:30h:12m:1s
1 Like

how would I do it in all months without changing it manually?

2 Likes

Use datastores. When the timer ends, have it increase the datastore by 1, and have months set to the datastore.

When the datastore hits 13, have it just set it back to 1, and have the year increase by 1.

2 Likes