# If with timeBetween

**URL:** <https://forum.inductiveautomation.com/t/if-with-timebetween/34244>\
**Category:** Ignition\
**Created:** [April 27, 2020, 5:40pm UTC](https://forum.inductiveautomation.com/t/if-with-timebetween/34244 "2020-04-27T17:40:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric.avila](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eric.avila](https://forum.inductiveautomation.com/u/eric.avila)\
**Post date:** [April 27, 2020, 5:40pm UTC](https://forum.inductiveautomation.com/t/if-with-timebetween/34244/1 "2020-04-27T17:40:03Z")

</div>

Hi

I want to get the time and return a value in an expression but don´t work can you help me?

the format of the tags are “MM-dd-yyyy hh:mm:ss”

i have this

if (timeBetween(dateFormat(now(), “MM:dd:aa hh:mm”), {[.]StartTime\_Shift1}, {[.]StartTime\_Shift2}),1,2)

this sends me “error configuration”

tried with this too

if (timeBetween(now(), {[.]StartTime\_Shift1}, {[.]StartTime\_Shift2}),1,2)  
but always sends me 2.

can you explain why don´t work?

regards

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 27, 2020, 6:10pm UTC](https://forum.inductiveautomation.com/t/if-with-timebetween/34244/2 "2020-04-27T18:10:53Z")

</div>

> [@eric.avila](#):
>
> the format of the tags are “MM-dd-yyyy hh:mm:ss”

If you are using 24-hour time format, use 'HH' instead. Otherwise, you need the meridiam (am/pm).

> [@eric.avila](#):
>
> dateFormat(now(), “MM:dd:aa hh:mm”

This is not a valid date. 'aa' should probably be 'dd'.

* * *

For shift times, I use a few string tags to hold just the time.  
[default]ShfitTimes/Shift1: `6:00 am`  
[default]ShfitTimes/Shift2: `2:00 pm`

```auto
if(timeBetween(dateFormat(now(),"h:mm:ss a"), 
               {[default]ShfitTimes/Shift1}, 
               {[default]ShfitTimes/Shift2}
               ),
    1,
    2
   )
```

---

<div class="post-metadata">

**Author:** ![eric.avila](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eric.avila](https://forum.inductiveautomation.com/u/eric.avila)\
**Post date:** [April 27, 2020, 8:52pm UTC](https://forum.inductiveautomation.com/t/if-with-timebetween/34244/3 "2020-04-27T20:52:17Z")

</div>

> [@JordanCClark](#):
>
> If you are using 24-hour time format, use ‘HH’ instead. Otherwise, you need the meridiam (am/pm)

Thanks Jordan

last question how did you make your tags with this values?

i use a tag startime\_shift1 type string concat two tags type integer i think this make the problem when i try to use the between

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 28, 2020, 11:06am UTC](https://forum.inductiveautomation.com/t/if-with-timebetween/34244/4 "2020-04-28T11:06:24Z")

</div>

Mine are currently looked up from a dataset, because different lines have different start times and break times, or are 2-shift vs 3 shift operations.

But when I first started, it was just an string expression tag with the value “6:00 am”. Memory tags didn’t exist in the old days. 😉

---

<div class="post-metadata">

**Author:** ![eric.avila](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eric.avila](https://forum.inductiveautomation.com/u/eric.avila)\
**Post date:** [April 30, 2020, 5:38pm UTC](https://forum.inductiveautomation.com/t/if-with-timebetween/34244/5 "2020-04-30T17:38:34Z")

</div>

Thanks, i resolved my problem.

regards

---

<div class="post-metadata">

**Author:** ![jay.meinke](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jay.meinke](https://forum.inductiveautomation.com/u/jay.meinke)\
**Post date:** [November 23, 2020, 6:45pm UTC](https://forum.inductiveautomation.com/t/if-with-timebetween/34244/6 "2020-11-23T18:45:45Z")

</div>

Jordan,  
I am using dateArithmetic(now(), -60, “minute” for last 60 for tag ScaleNow1hr, but when the second shift starts at 2pm, there is a spillover. Where do i put your code in to pull sql data in a tag?

if(timeBetween(dateFormat(now(),“h:mm:ss”),  
{[default]ShfitTimes/Shift1},  
{[default]ShfitTimes/Shift2}  
),  
1,  
2  
)  
**:mycode for last 60min**  
SELECT sum(actual) as ‘sum’ FROM mydatabase  
WHERE  
T\_stamp \>= ‘{[.]ScaleMinus1hr}’ and T\_Stamp \< ‘{[.]ScaleNow}’

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 26, 2020, 4:08pm UTC](https://forum.inductiveautomation.com/t/if-with-timebetween/34244/7 "2020-11-26T16:08:40Z")

</div>

Hi Jay, sorry for the late reply.

Been on vacation the past few days, and today is more of a family day 😉 .

The expressions I posted above isn’t the best fit for what you’re needing, but I can post something tomorrow morning. Probably be a couple more expressions to handle the demarcation of shifts.

Shh, my wife is coming. Later!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 26, 2020, 11:34pm UTC](https://forum.inductiveautomation.com/t/if-with-timebetween/34244/8 "2020-11-26T23:34:35Z")

</div>

Okay, this one is a bit more involved. I switched this to seconds, as the seconds portion of the date still fluctuated when using minutes.

ScaleMinus1hr (uses the smaller absolute value between start-of-shift to now or 3600 seconds):

```auto
addSeconds(now(),
		   max(case({[.]Current Shift},
		 	  		1, secondsBetween(toDate(dateFormat(now(), 'hh:mm:ss a')), toDate({[.]Shift1})),
	 	 	   		2, secondsBetween(toDate(dateFormat(now(), 'hh:mm:ss a')), toDate({[.]Shift2})),
	 	 	   		0
	 		  	   ),
	 		   -3600
	 		  )
		   )

```

Query (Note that you don’t need to specify less than now, as there will be no data that exists above now, How’s that for existential?):

```sql
SELECT sum(actual) as ‘sum’ FROM mydatabase
WHERE
T_stamp >= ‘{[.]ScaleMinus1hr}’

```

Hope this helps!
