# Date comparison in TagChange script

**URL:** <https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636>\
**Category:** Ignition\
**Tags:** scripting, designer\
**Created:** [April 3, 2025, 9:33am UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636 "2025-04-03T09:33:16Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose\_Hervas\_Chico](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose_hervas_chico/32/69260_2.png) [@Jose\_Hervas\_Chico](https://forum.inductiveautomation.com/u/Jose_Hervas_Chico)\
**Post date:** [April 3, 2025, 9:33am UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/1 "2025-04-03T09:33:16Z")

</div>

Good morning,

I am trying to do a simple script but to work with dates in Jython is driving me nuts.  
I only want to write the current date in a dateTime Tag only if this Datetime Tag is 0 (I mean, 1970-01-01 ....)

I tried several things such as:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/5/c50b6aacf569b8eb712b5b29ba6c4e1b0d79fe1b.png)

but the If clause is not working.

I tried also with == '1970-01-01 00:00:00 am'  
or even getting the YEAR and comparing it with the int 1970.  
None of the mentioned has worked...

Can anyone give me light on this doom question?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![Jose\_Hervas\_Chico](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose_hervas_chico/32/69260_2.png) [@Jose\_Hervas\_Chico](https://forum.inductiveautomation.com/u/Jose_Hervas_Chico)\
**Post date:** [April 3, 2025, 9:42am UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/2 "2025-04-03T09:42:47Z")

</div>

Even this is not working, how is this possible?

```python
if currentValue.value <> previousValue.value and currentValue.value and not initialChange:
	TimeHoist= system.tag.readBlocking("[.]HoistingTime")[0].value
	if str(system.date.getYear(TimeHoist)) == '1970':	
		system.tag.writeBlocking("[.]HoistingTime", system.date.now())

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/9/79b6daf7f8c78285206108651145e217d6433e14.png)

---

<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:** [April 3, 2025, 12:00pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/3 "2025-04-03T12:00:48Z")

</div>

One possible issue is this:

```python
if currentValue.value <> previousValue.value

```

on the first execution `previousValue` will be `None` and trying to dereference `value` will fail.

Can you add some print statements that print the value of `TimeHoist` before doing the comparison?

---

<div class="post-metadata">

**Author:** ![Jose\_Hervas\_Chico](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose_hervas_chico/32/69260_2.png) [@Jose\_Hervas\_Chico](https://forum.inductiveautomation.com/u/Jose_Hervas_Chico)\
**Post date:** [April 3, 2025, 12:36pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/4 "2025-04-03T12:36:02Z")

</div>

Thanks Kevin, of course I can, but where can I see the print results if I program it on a TagChange script?

---

<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:** [April 3, 2025, 12:37pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/5 "2025-04-03T12:37:39Z")

</div>

In the gateway log files (wrapper.log), or in the gateway logs webpage if you use system.util.getLogger

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 3, 2025, 12:44pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/6 "2025-04-03T12:44:33Z")

</div>

In your `if` statement, the check of `not initialChange` needs to be **first** , to prevent null pointer errors on `previousValue`.

---

<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 3, 2025, 12:44pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/7 "2025-04-03T12:44:42Z")

</div>

I would move the not initialChange to the front of the ifs. That would catch the Nones on first execution.

---

<div class="post-metadata">

**Author:** ![Jose\_Hervas\_Chico](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose_hervas_chico/32/69260_2.png) [@Jose\_Hervas\_Chico](https://forum.inductiveautomation.com/u/Jose_Hervas_Chico)\
**Post date:** [April 3, 2025, 2:06pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/8 "2025-04-03T14:06:01Z")

</div>

To avoid wrong diagnose, I deleted the first if, now the code looks like a simple if:

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	TimeHoist= system.tag.readBlocking("[.]HoistingTime")[0].value
	
	if str(system.date.getYear(TimeHoist)) == '1970':	
		system.tag.writeBlocking("[.]HoistingTime", system.date.now())

```

even without the first if, the script is not working ☹

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [April 3, 2025, 2:16pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/9 "2025-04-03T14:16:07Z")

</div>

Why convert it to a string? Just compare the returned integer value to an integer date?

```python
if system.date.getYear(TimeHoist)) == 1970:

```

Also, you're changing this script in a UDT definition, are you sure that there is an actual instance tag of the UDT defined and changing?

---

<div class="post-metadata">

**Author:** ![Jose\_Hervas\_Chico](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose_hervas_chico/32/69260_2.png) [@Jose\_Hervas\_Chico](https://forum.inductiveautomation.com/u/Jose_Hervas_Chico)\
**Post date:** [April 3, 2025, 3:47pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/10 "2025-04-03T15:47:58Z")

</div>

Finally, debugging with the Logger, I found that the default value is not "1970" but "1969".

In the tag tree the value is shown as "1970" but the gateway interpret the default DateTime as "1969".

Now I compared with "1969" and works fine.

thanks all for your help!!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 3, 2025, 3:50pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/11 "2025-04-03T15:50:16Z")

</div>

Perhaps, to be less fragile and less inscrutable for the next person behind you:

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	TimeHoist= system.tag.readBlocking("[.]HoistingTime")[0].value
	
	if system.date.getMillis(TimeHoist) == 0:	
		system.tag.writeBlocking("[.]HoistingTime", system.date.now())

```

Or if that doesn't work:  
`if TimeHoist.time == 0:`

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 3, 2025, 3:52pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/12 "2025-04-03T15:52:13Z")

</div>

> [@paul-griffith](#):
>
> TimeHoist.time == 0

That's what I would use.

> [@Jose\_Hervas\_Chico](#):
>
> not "1970" but "1969".

That's an artifact of living in a western time zone. Checking the milliseconds avoids time zone problems.

---

<div class="post-metadata">

**Author:** ![Jose\_Hervas\_Chico](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jose_hervas_chico/32/69260_2.png) [@Jose\_Hervas\_Chico](https://forum.inductiveautomation.com/u/Jose_Hervas_Chico)\
**Post date:** [April 3, 2025, 5:02pm UTC](https://forum.inductiveautomation.com/t/date-comparison-in-tagchange-script/104636/13 "2025-04-03T17:02:43Z")

</div>

Great!! thanks a lot!! now much more clearer for the future reader 🙂
