KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Code snippet to check if a time is between two other times
PRODUCT: 4D | VERSION: 15 | PLATFORM: Mac & Win
Published On: November 17, 2015

The following code snippet can be used for checking if a given time value is between two other time values on a 24 hour clock

// BOOL = util_isBetweenTimes ($this_ti;$start_ti;$end_ti)
// $1-$3 := Time
// $0 := Boolean
If (Count parameters=3)
   C_BOOLEAN($0)
   C_TIME($1;$2;$3;$this_ti;$start_ti;$end_ti)
   $this_ti:=$1%?24:00:00?
   $start_ti:=$2%?24:00:00?
   $end_ti:=$3%?24:00:00?
   If ($start_ti<=$end_ti)
      $0:=(($start_ti<=$this_ti) & ($this_ti<=$end_ti))
   Else
      $0:=(($start_ti<=$this_ti) | ($this_ti<=$end_ti))
   End if
End if
The Modulo (%) is used to make sure that the time is on a 24:00:00 scale. This would convert inputs like 25:00:00 to be 01:00:00

Here we check if 12:00:00 is between 20:00:00 and 08:00:00
C_TIME($this_ti;$start_ti;$end_ti)
$start_ti:=?20:00:00?
$end_ti:=?08:00:00?
$this_ti:=?12:00:00?
ALERT(String(util_isBetweenTimes ($this_ti;$start_ti;$end_ti)))
This returns False.

Here we check if 06:00:00 is between 20:00:00 and 08:00:00
C_TIME($this_ti;$start_ti;$end_ti)
$start_ti:=?20:00:00?
$end_ti:=?08:00:00?
$this_ti:=?06:00:00? //true
ALERT(String(util_isBetweenTimes ($this_ti;$start_ti;$end_ti)))
This returns True.