# Using \`timeBetween\` seems to be returning \`false\` when it should be returning \`true\`

**URL:** <https://forum.inductiveautomation.com/t/using-timebetween-seems-to-be-returning-false-when-it-should-be-returning-true/26664>\
**Category:** Ignition\
**Created:** [July 18, 2019, 1:43pm UTC](https://forum.inductiveautomation.com/t/using-timebetween-seems-to-be-returning-false-when-it-should-be-returning-true/26664 "2019-07-18T13:43:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![md.szyman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/md.szyman/32/9240_2.png) [@md.szyman](https://forum.inductiveautomation.com/u/md.szyman)\
**Post date:** [July 18, 2019, 1:43pm UTC](https://forum.inductiveautomation.com/t/using-timebetween-seems-to-be-returning-false-when-it-should-be-returning-true/26664/1 "2019-07-18T13:43:49Z")

</div>

I’m using `timeBetween` with full date objects and from what I’ve tested with manual values, even if the dates don’t match up, as long as the time is within the range, it _should_ work. However, I’m getting all three values for the `timeBetween` expression function from tags, and their values are:

```auto
date = Fri May 10 10:00:00 CDT 2019
startDate = Mon Jul 15 07:00:00 CDT 2019
endDate = Mon Jul 15 19:00:00 CDT 2019

```

(values copied from the tag browser)

However, with these tags, `timeBetween` is returning false. Is this a bug?

EDIT: Whenever I set `date` to have the same date as the start/end dates, it works. But shouldn’t `timeBetween` only be comparing the times, not the date values?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 18, 2019, 1:52pm UTC](https://forum.inductiveautomation.com/t/using-timebetween-seems-to-be-returning-false-when-it-should-be-returning-true/26664/2 "2019-07-18T13:52:23Z")

</div>

Hmm. Neither the docs nor the function implementation suggests that the Y/M/D portion of a date will be ignored.

and its implementation hasn’t changed meaningfully in years.

---

<div class="post-metadata">

**Author:** ![md.szyman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/md.szyman/32/9240_2.png) [@md.szyman](https://forum.inductiveautomation.com/u/md.szyman)\
**Post date:** [July 18, 2019, 1:54pm UTC](https://forum.inductiveautomation.com/t/using-timebetween-seems-to-be-returning-false-when-it-should-be-returning-true/26664/3 "2019-07-18T13:54:11Z")

</div>

So that means there’s no meaningful difference between `timeBetween` and `dateIsBetween`? Except I guess that `timeBetween` will work without a date component present?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 18, 2019, 1:59pm UTC](https://forum.inductiveautomation.com/t/using-timebetween-seems-to-be-returning-false-when-it-should-be-returning-true/26664/5 "2019-07-18T13:59:42Z")

</div>

Yes, `timeBetween` works when the Y/M/D is not specified by setting those components to Jan 1, 1970 and then doing the comparison as normal.

---

<div class="post-metadata">

**Author:** ![md.szyman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/md.szyman/32/9240_2.png) [@md.szyman](https://forum.inductiveautomation.com/u/md.szyman)\
**Post date:** [July 18, 2019, 2:03pm UTC](https://forum.inductiveautomation.com/t/using-timebetween-seems-to-be-returning-false-when-it-should-be-returning-true/26664/6 "2019-07-18T14:03:23Z")

</div>

Ah okay, I see. Apologies for misunderstanding things.

I’ve made this work by using `dateFormat` to format all the parameters to the `timeBetween` function as `hh:mm:ss a` format, so just throwing out the date portion effectively.

Thanks for the clarification!

---

<div class="post-metadata">

**Author:** ![md.szyman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/md.szyman/32/9240_2.png) [@md.szyman](https://forum.inductiveautomation.com/u/md.szyman)\
**Post date:** [July 18, 2019, 2:26pm UTC](https://forum.inductiveautomation.com/t/using-timebetween-seems-to-be-returning-false-when-it-should-be-returning-true/26664/7 "2019-07-18T14:26:36Z")

</div>

Just to add a little footnote here, my goal with this was to check if a time falls within a “shift”, and that includes shifts where the start time is AFTER the end time (i.e: night shift, goes from 7PM to 7AM)

Checking this in the expression language takes a bit of trickery, but it’s not terrible. Basically the expression I wrote checks if the start time is after the end time, and if it is, compares by swapping the start/end times in the `timeBetween` function and subtracting off 12 hours from the current time/time being checked. This works with time/date items, since it throws away any date portion of the time. (This just kind of assumes that `{StartDate}` and `{EndDate}` don’t have mismatching date components that would cause this checking to fail)

Here’s the function for anyone who might find it useful:

```auto
if(dateIsAfter({StartDate}, {EndDate}),
	timeBetween(
		dateFormat(addHours({CurrentDate}, -12), "hh:mm:ss a"),
		dateFormat({EndDate}, "hh:mm:ss a"),
		dateFormat({StartDate}, "hh:mm:ss a")),
	timeBetween(
		dateFormat({CurrentDate}, "hh:mm:ss a"), 
		dateFormat({StartDate}, "hh:mm:ss a"), 
		dateFormat({EndDate}, "hh:mm:ss a")))

```

---

<div class="post-metadata">

**Author:** ![Adarsh\_Shelar](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adarsh\_Shelar](https://forum.inductiveautomation.com/u/Adarsh_Shelar)\
**Post date:** [February 23, 2023, 1:10pm UTC](https://forum.inductiveautomation.com/t/using-timebetween-seems-to-be-returning-false-when-it-should-be-returning-true/26664/8 "2023-02-23T13:10:03Z")

</div>

Hi,  
we Have Done with the 3shift timings. but working with the 3shifr exactly but getting the point  
Exactly In shift2 whenever it goes to the Next day starting time at the 00:00 Shift2 is not getting enable.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/a/3a2a41409e27afb271ea1857201273cb4dc3d42f.png)  
as this above-mentioned image working only for the shift 1 & 3 not for the shift 2. so what might the Issue.How we can resolve it
