• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua os.time()

andu

Sold 649 scripts, 25 maps and 9 events!
Joined
Aug 7, 2009
Messages
978
Solutions
17
Reaction score
373
GitHub
olrios
Twitch
jamagowy
Hello.

I'm stuck with one thing. I'm going to make a Christmas Event which starts in 18/12 and ends 4/01 (DD/MM). How to check in lua does actual day is in range?
 
Hello.

I'm stuck with one thing. I'm going to make a Christmas Event which starts in 18/12 and ends 4/01 (DD/MM). How to check in lua does actual day is in range?
Maybe something like this can work
Code:
local startDate = os.time{year=2016, month=12, day=2, hour=10, sec=0}
local endDate = os.time{year=2016, month=12, day=17, hour=21, sec=0}
local currentDate = os.time()

if (currentDate >= startDate and currentDate <= endDate) then
    print('BETWEEN')
end
 
Event starts at 18th December and ends 4th January. Also script shouldn't check for year, becouse I want to make it work in every year without doing any changes.

I did sth like this:
Code:
local starts = {"18", "12"} -- DD, MM
local ends = {"04", "01"} -- DD, MM

[...]

if os.time("%m") >= starts[2] or os.time("%m") <= ends[2] then -- check for months
     if os.time("%d") >= starts[1] or os.time("%d") <= ends[1] then -- check for days
          ---
     end
end

Second line have bad logic. Becouse script may continue even if date is: 03/12 (DD/MM)

#edit, solved it with this code:

Code:
        local starts = {"18", "12"} -- DD, MM
        local ends = {"04", "01"} -- DD, MM

[...]

        local check = 0
        if os.time("%m") == starts[2] and os.time("%d") >= starts[1] then
            check = 1
        elseif os.time("%m") == ends[2] and os.time("%d") <= ends[1] then
            check = 1
        elseif os.time("%m") > starts[2] and os.time("%m") < ends[2] then
            check = 1
        end
        if check == 1 then
            -- start event
        end
 
Last edited:
Back
Top