# Time Comparison

**URL:** <https://forum.inductiveautomation.com/t/time-comparison/1337>\
**Category:** Archived Topics\
**Created:** [October 16, 2009, 3:20pm UTC](https://forum.inductiveautomation.com/t/time-comparison/1337 "2009-10-16T15:20:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FieldsEng](https://avatars.discourse-cdn.com/v4/letter/f/ed655f/32.png) [@FieldsEng](https://forum.inductiveautomation.com/u/FieldsEng)\
**Post date:** [October 16, 2009, 3:20pm UTC](https://forum.inductiveautomation.com/t/time-comparison/1337/1 "2009-10-16T15:20:16Z")

</div>

Hi, I’m new to this scripting language and am having trouble comparing time strings. This is what I want to do (written from my Visual Basic background):

If Time \>= “7:00” and Time \<= “14:59” then Shift = “1”

Trying this for FactoryPMI I got  
if (DateFormat(Now(), “H:mm”) = ‘7:00’, “1”,“2”)  
to work, but an error if I try  
if (DateFormat(Now(), “H:mm”) \> ‘7:00’, “1”,“2”)  
since I’m comparing a string and not an integer.

So…I guess my question is, how do I compare a time string in an expression?  
Thanks.

---

<div class="post-metadata">

**Author:** ![FieldsEng](https://avatars.discourse-cdn.com/v4/letter/f/ed655f/32.png) [@FieldsEng](https://forum.inductiveautomation.com/u/FieldsEng)\
**Post date:** [October 16, 2009, 3:54pm UTC](https://forum.inductiveautomation.com/t/time-comparison/1337/2 "2009-10-16T15:54:31Z")

</div>

I figured out another way to do this:

if (dateExtract(now(), “hour”) \>= 7 && dateExtract(now(), “hour”) \<= 14, “1”, “0”)

Now my question is, how do I do nested, or if/else statements in FactoryPMI?

ie,  
if (dateExtract(now(), “hour”) \>= 7 && dateExtract(now(), “hour”) \<= 14, “1”, “0”)  
else if (dateExtract(now(), “hour”) \>= 15 && dateExtract(now(), “hour”) \<= 22, “2”, “0”)

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [October 16, 2009, 4:17pm UTC](https://forum.inductiveautomation.com/t/time-comparison/1337/3 "2009-10-16T16:17:06Z")

</div>

Just to be clear, you’re talking about the expression language, not the scripting language.

To nest if statements in the expression language, you just use the second “if” as the THEN expression, like so:

`if (dateExtract(now(), "hour") >= 7 && dateExtract(now(), "hour") <= 14,
   "1",
   if (dateExtract(now(), "hour") >= 15 && dateExtract(now(), "hour") <= 22,
      "2",
      "0")
   )`

---

<div class="post-metadata">

**Author:** ![FieldsEng](https://avatars.discourse-cdn.com/v4/letter/f/ed655f/32.png) [@FieldsEng](https://forum.inductiveautomation.com/u/FieldsEng)\
**Post date:** [October 16, 2009, 4:30pm UTC](https://forum.inductiveautomation.com/t/time-comparison/1337/4 "2009-10-16T16:30:26Z")

</div>

Great, thanks.
