# Tag Timestamp difference in minutes

**URL:** <https://forum.inductiveautomation.com/t/tag-timestamp-difference-in-minutes/69499>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [February 6, 2023, 7:32pm UTC](https://forum.inductiveautomation.com/t/tag-timestamp-difference-in-minutes/69499 "2023-02-06T19:32:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilikebeer](https://avatars.discourse-cdn.com/v4/letter/i/eb8c5e/32.png) [@ilikebeer](https://forum.inductiveautomation.com/u/ilikebeer)\
**Post date:** [February 6, 2023, 7:32pm UTC](https://forum.inductiveautomation.com/t/tag-timestamp-difference-in-minutes/69499/1 "2023-02-06T19:32:30Z")

</div>

Have tags where operators enter readings. I want to evaluate the difference in their values in relation to the elapsed time. Have everything else working except how to find the difference in the current time stamp and the previous timestamp. Tried different ways of DATEDIFF and some valueChanged expression but i'm not getting anywhere and would appreciate some advice or direction to examples.  
Thanks.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [February 6, 2023, 7:42pm UTC](https://forum.inductiveautomation.com/t/tag-timestamp-difference-in-minutes/69499/2 "2023-02-06T19:42:39Z")

</div>

I'm not sure if your tag is holding a timestamp value, or you are trying to use the timestamp from the qualified tag path. Either way

Since you are using, I assume the tag value is the datetime, you should be able to use a `valueChanged` script (which is not an expression).

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if not initialChange:
		seconds = system.date.secondsBetween(previousValue.value, currentValue.value)
		# write to another tag or do something else with the time diff here

```

If you need to do heavier calculations or expect several of these events, then move the script from the `valueChanged` script to a tag change gateway script which can handle more events in fast succession.

---

<div class="post-metadata">

**Author:** ![ilikebeer](https://avatars.discourse-cdn.com/v4/letter/i/eb8c5e/32.png) [@ilikebeer](https://forum.inductiveautomation.com/u/ilikebeer)\
**Post date:** [February 6, 2023, 8:18pm UTC](https://forum.inductiveautomation.com/t/tag-timestamp-difference-in-minutes/69499/3 "2023-02-06T20:18:40Z")

</div>

That looks AWESOME. Not exactly what i was looking for but what you've written looks perfect.  
Won't be able to see till tomorrow unfortunately, but my thanks!

---

<div class="post-metadata">

**Author:** ![ilikebeer](https://avatars.discourse-cdn.com/v4/letter/i/eb8c5e/32.png) [@ilikebeer](https://forum.inductiveautomation.com/u/ilikebeer)\
**Post date:** [February 7, 2023, 12:44pm UTC](https://forum.inductiveautomation.com/t/tag-timestamp-difference-in-minutes/69499/4 "2023-02-07T12:44:02Z")

</div>

Finally had a chance to try this, this morning and I'm doing something wrong, or more likely, i didn't explain the problem correctly. Forgive me, i just know enough to be dangerous (PLC programmer).  
I created tags for values enter by an operator. I have more tags finding the value differences and other information needed. That all works fine. What i am looking for is to find the difference in the timestamps (minutes) for when those values were changed. Reading your advice, will i have to create another folder of tags for the timestamps to be moved into the Value or is there a better way?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [February 7, 2023, 1:02pm UTC](https://forum.inductiveautomation.com/t/tag-timestamp-difference-in-minutes/69499/5 "2023-02-07T13:02:35Z")

</div>

If your tag's value is not a datetime, then just use the timestamp portion of the qualified value. Every tag is a qualified value that has a value, timestamp, and quality component to them.

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if not initialChange:
		minutes = system.date.secondsBetween(previousValue.timestamp, currentValue.timestamp) / 60.0
		# write to another tag or do something else with the time diff here

```

You can use `system.date.minutesBetween(x,y)` but it's less granular, I like using seconds then convert to the units I want when you need a little more precision.

---

<div class="post-metadata">

**Author:** ![ilikebeer](https://avatars.discourse-cdn.com/v4/letter/i/eb8c5e/32.png) [@ilikebeer](https://forum.inductiveautomation.com/u/ilikebeer)\
**Post date:** [February 7, 2023, 2:04pm UTC](https://forum.inductiveautomation.com/t/tag-timestamp-difference-in-minutes/69499/6 "2023-02-07T14:04:46Z")

</div>

This is perfect! Thank you for excellent assistance.
