# How to manipulate datetime returned from client tag?

**URL:** <https://forum.inductiveautomation.com/t/how-to-manipulate-datetime-returned-from-client-tag/12770>\
**Category:** Ignition\
**Created:** [March 9, 2017, 4:52pm UTC](https://forum.inductiveautomation.com/t/how-to-manipulate-datetime-returned-from-client-tag/12770 "2017-03-09T16:52:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 9, 2017, 4:52pm UTC](https://forum.inductiveautomation.com/t/how-to-manipulate-datetime-returned-from-client-tag/12770/1 "2017-03-09T16:52:28Z")

</div>

Further to [How to have a common date range across windows](https://inductiveautomation.com/forum/viewtopic.php?f=94&t=16785) question.

[code]import datetime

# ctEndDate has bi-directional binding to a calendar component.

endDate = system.tag.read("[Client]ctEndDate")  
print endDate

# results in: [Thu Mar 09 23:59:59 GMT 2017, Good, Thu Mar 09 12:20:38 GMT 2017]

[/code]

How do I work with this value to calculate time difference and output the result to another client tag? I understand (mostly) how timedelta works. The datetime functions don’t seem to understand the data format returned by system.tag.read.

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [March 9, 2017, 5:38pm UTC](https://forum.inductiveautomation.com/t/how-to-manipulate-datetime-returned-from-client-tag/12770/2 "2017-03-09T17:38:10Z")

</div>

```auto
endDate = system.tag.read("[Client]ctEndDate").value
```

---

<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:** [March 9, 2017, 6:44pm UTC](https://forum.inductiveautomation.com/t/how-to-manipulate-datetime-returned-from-client-tag/12770/3 "2017-03-09T18:44:42Z")

</div>

Two things:

1. system.tag.read() returns a “Qualified Value” that includes quality and timestamp, not just the actual tag value. Use endDate.value to get the tag value itself.
2. Since Ignition is written in Java, it uses the [java.util.Date](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) datatype everywhere, not the python datetime datatype. Consider using [java.util.Calendar](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html) to manipulate your dates instead of the python date difference methods – it’ll save you a lot of grief.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 10, 2017, 2:45pm UTC](https://forum.inductiveautomation.com/t/how-to-manipulate-datetime-returned-from-client-tag/12770/4 "2017-03-10T14:45:34Z")

</div>

@MMaynardUSG: That was the kind of prompt I was hoping for, thanks. I’m still missing something in my understanding.

```auto
import datetime
eDate = system.tag.read("[Client]ctEndDate").value
print "eDate.value: ", eDate
print "eDate year month day: ", eDate.year, eDate.month, eDate.day
```

results in

```auto
eDate.value: Thu Mar 09 23:59:59 gmt 2017
eDate year month day: 117 2 4
```

Why is this?

@pturmel: Thanks for the tips. I had an unsuccessful look around for an example of using the Java libraries. I’ll have to do some more research.

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [March 10, 2017, 3:22pm UTC](https://forum.inductiveautomation.com/t/how-to-manipulate-datetime-returned-from-client-tag/12770/5 "2017-03-10T15:22:09Z")

</div>

Java dates can be weird.

The year is the number of years since 1900. So if you print year+1900, you’ll be happier with the result

The month is 0 indexed, so the third month (March) is 2.

Day is actually day of the week. You probably wanted to print date.

The docs can be found at [docs.oracle.com/javase/8/docs/a … /Date.html](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) Personally I find it easier to use the Date to create a Java [Calendar](https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) object and use Calendar’s constants to get what I want. But that’s probably because I’m a Java programmer, not a Python programmer. 🙂

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 10, 2017, 4:17pm UTC](https://forum.inductiveautomation.com/t/how-to-manipulate-datetime-returned-from-client-tag/12770/6 "2017-03-10T16:17:14Z")

</div>

Thanks, Kathy. I’m in business.  
I’ve only one date-diff calculation to do for this project so I’ll stick with Python for now. Points about switching to Java are taken.

For anyone else this will work in the script console:

[code]import datetime  
eDate = system.tag.read("[Client]ctEndDate").value  
daysToSpan = 5

print "eDate.value: ", eDate  
print "eDate year month day: ", eDate.year + 1900, eDate.month + 1, eDate.date  
print "5 days span start date: ", datetime.date(eDate.year + 1900, eDate.month + 1, eDate.date) - datetime.timedelta(days = daysToSpan - 1)[/code]  
… giving output …

`
eDate.value: Thu Mar 09 23:59:59 GMT 2017
eDate year month day: 2017 3 9
5 days span start date: 2017-03-05`

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [March 10, 2017, 4:42pm UTC](https://forum.inductiveautomation.com/t/how-to-manipulate-datetime-returned-from-client-tag/12770/7 "2017-03-10T16:42:09Z")

</div>

And if I’d had enough caffeine this morning, I could also have recommended the scripting functions we provided to make this easier. (As I said, I’m a Java person not Python. 😊 )

[system.date.get\*](https://docs.inductiveautomation.com:8443/pages/viewpage.action?pageId=6036600) and [system.date.add\*](https://docs.inductiveautomation.com:8443/pages/viewpage.action?pageId=6036593)
