'How to check if current time is between two others?

What is the best way to check in Lua if the current time is between intervals?

I.e. between 5am and 8am or 11pm and 1am?



Solution 1:[1]

os.date("*t", os.time()) gets a table representing the current time, which has a hour field (range 0 - 23), a min field, a sec field.

local current = os.date("*t", os.time())
print(current.hour, current.min)

Compare the time with hour * 60 + min.

Solution 2:[2]

You could do some arithmetic to compare each value based on minutes:

local function getMinutes(hours,minutes) 
    return (hours*60)+minutes
end

local value1 = getMinutes(time1.hours,time1.minutes)
local value2 = getMinutes(time2.hours,time2.minutes)
local currentTime = getMinutes(tonumber(os.date("%H"),tonumber(os.date("%M")))

local isBetween = false

if (currentTime >= value1 and currentTime <= value2) or (currentTime >= value2 and currentTime <= value1) then
    isBetween = true
end

Solution 3:[3]

The above function did not work for me. It does not work if the second time is on the next day. If extended the script, maybe it helps someone else too.

local StartTimeHours=21
local StartTimeMinutes=11
local StopTimeHours=8
local StopTimeMinutes=30

time = os.date("*t")
local curhour=time.hour
local curmin=time.min

local function getMinutes(hours, minutes) 
    return (hours*60)+minutes
end

local function IsTimeBetween(StartH, StartM, StopH, StopM, TestH, TestM)
    if (StopH < StartH) then -- add 23 hours if endhours < starthours
        local StopHOrg=StopH
        StopH=StopH+23
        if (TestH <= StopHOrg) then -- if endhours has increased the currenthour should also increase
            TestH=TestH+23
        end
    end

    local StartTVal = getMinutes(StartH, StartM)
    local StopTVal = getMinutes(StopH, StopM)
    local curTVal = getMinutes(TestH, TestM)
    if (curTVal >= StartTVal and curTVal <= StopTVal) then
        return true
    else
        return false
    end
end    

local isBetween = IsTimeBetween(StartTimeHours, StartTimeMinutes, StopTimeHours, StopTimeMinutes, curhour, curmin)
commandArray = {}

if (isBetween) then
    print("Yep: ".. curhour..":".. curmin .. " is between " .. StartTimeHours .. ":"..StartTimeMinutes .." and "..StopTimeHours..":"..StopTimeMinutes)
else
    print("No: "..curhour..":".. curmin.." is not between "..StartTimeHours..":"..StartTimeMinutes.." and "..StopTimeHours..":"..StopTimeMinutes)
end

return commandArray

Gr, Ronald

Solution 4:[4]

This is Ronalds solution but tweaked (23 hour day) and added function to simply check with current time:

local function getMinutes(hours, minutes) 
    return (hours*60)+minutes
end

local function IsTimeBetween(StartH, StartM, StopH, StopM, TestH, TestM)
    if (StopH < StartH) then -- add 24 hours if endhours < starthours
        local StopHOrg=StopH
        StopH = StopH + 24
        if (TestH <= StopHOrg) then -- if endhours has increased the currenthour should also increase
            TestH = TestH + 24
        end
    end

    local StartTVal = getMinutes(StartH, StartM)
    local StopTVal = getMinutes(StopH, StopM)
    local curTVal = getMinutes(TestH, TestM)
    return (curTVal >= StartTVal and curTVal <= StopTVal)
end    

local function IsNowBetween(StartH,StartM,StopH,StopM)
  local time = os.date("*t")
  return IsTimeBetween(StartH, StartM, StopH, StopM, time.hour, time.min)
end

I use this to turn different lights with motion sensor in my house. When its early evening i turn lights to 100%, and at night to 5% (I have warm white and cold white LED strips connected to RGBW controller R and G connnectors):

local LuxTreshold=60
local StartTimeHours=20
local StartTimeMinutes=15
local StopTimeHours=8
local StopTimeMinutes=00

if (tonumber(fibaro:getValue(28, "value")) < LuxTreshold )
then
    if (IsNowBetween(StartTimeHours, StartTimeMinutes, StopTimeHours, StopTimeMinutes))
    then
        fibaro:call(10, "setColor", "16","0","0","0")
        fibaro:call(10, "turnOn")
    else
        fibaro:call(10, "setColor", "255","255","0","0")
        fibaro:call(10, "turnOn")
    end
end

Solution 5:[5]

I found this simple way:

local currentMinutesPastMidnight = (tonumber(os.time("%H))*60 + tonumber(os.time(%M))
local startTime = (7*60)+30 -- 07:30 in minutes past midnight
local endTime = (21*60) +30 -- 21:30 

if currentMinutesPastMidnight<= startTime and currentMinutesPastMidnight >= end time then
   print("day time")
else
   print("Night Time")
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 BuddyD
Solution 3 Ronald
Solution 4 Edward Trapp
Solution 5 Jeremy Caney