# Passing calendar date into Python script

**URL:** <https://forum.inductiveautomation.com/t/passing-calendar-date-into-python-script/5552>\
**Category:** Ignition\
**Created:** [December 10, 2012, 3:11pm UTC](https://forum.inductiveautomation.com/t/passing-calendar-date-into-python-script/5552 "2012-12-10T15:11:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Step7](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Step7](https://forum.inductiveautomation.com/u/Step7)\
**Post date:** [December 10, 2012, 3:11pm UTC](https://forum.inductiveautomation.com/t/passing-calendar-date-into-python-script/5552/1 "2012-12-10T15:11:55Z")

</div>

I finally wanted to use one of the calendar components, and am having trouple passing in the startDate into Python string formatting. I’ve tried al kinds of things, but the end result is an arithmetic overflow error. All I want to do is something like this:

```auto
"SELECT Machine FROM MyTable WHERE timeStamp>=%d"%(event.source.parent.getComponent('Date Range 1').startDate)
```

I’ve tried using ‘%s’, various date formats, etc, but I’m obviously missing something simple.

---

<div class="post-metadata">

**Author:** ![James.Lorenz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/james.lorenz/32/3290_2.png) [@James.Lorenz](https://forum.inductiveautomation.com/u/James.Lorenz)\
**Post date:** [December 10, 2012, 5:09pm UTC](https://forum.inductiveautomation.com/t/passing-calendar-date-into-python-script/5552/2 "2012-12-10T17:09:03Z")

</div>

For the snippet of code you posted, %s or ‘%s’ should both work, it just depends on if you want the inserted string to have single quotes surrounding it or not. The arithmetic overflow error is odd, do you mind posting more of the code so I can get a better context of what’s going on? Thanks.

---

<div class="post-metadata">

**Author:** ![Step7](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Step7](https://forum.inductiveautomation.com/u/Step7)\
**Post date:** [December 10, 2012, 6:09pm UTC](https://forum.inductiveautomation.com/t/passing-calendar-date-into-python-script/5552/3 "2012-12-10T18:09:45Z")

</div>

This is what I had at the beginning, and I get a type conversion error, most likely because of the Java date format. I then tried converting the Java date to other formats, which resulted in the overflow errors. To add to the confusion, when I do a ‘print startDate’ from the Calendar control, the format is different from that in the designer.

Anyway, let’s see if we can get something like this to work:

```auto
d=event.source.parent.getComponent('Date Range').startDate
q="SELECT Machine FROM DataMachines WHERE ts >= '%s'"%(d)
print q
system.db.runQuery(q)
```

---

<div class="post-metadata">

**Author:** ![James.Lorenz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/james.lorenz/32/3290_2.png) [@James.Lorenz](https://forum.inductiveautomation.com/u/James.Lorenz)\
**Post date:** [December 10, 2012, 6:24pm UTC](https://forum.inductiveautomation.com/t/passing-calendar-date-into-python-script/5552/4 "2012-12-10T18:24:19Z")

</div>

Have you tried using runPrepQuery? It should properly handle the string substitution for you:

```auto
system.db.runPrepQuery("SELECT Machine FROM DataMachines WHERE ts>=?", [event.source.parent.getComponent('Date Range').startDate])
```

---

<div class="post-metadata">

**Author:** ![Step7](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Step7](https://forum.inductiveautomation.com/u/Step7)\
**Post date:** [December 10, 2012, 6:51pm UTC](https://forum.inductiveautomation.com/t/passing-calendar-date-into-python-script/5552/5 "2012-12-10T18:51:30Z")

</div>

No, actually I was writing ‘q’ to a string dynamic variable, and then binding this parameter to a dataset through a SQL binding (I construct the query dynamically based on a multi-selection button). Is there a way to run a PrepQuery through a property binding?

---

<div class="post-metadata">

**Author:** ![James.Lorenz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/james.lorenz/32/3290_2.png) [@James.Lorenz](https://forum.inductiveautomation.com/u/James.Lorenz)\
**Post date:** [December 10, 2012, 7:23pm UTC](https://forum.inductiveautomation.com/t/passing-calendar-date-into-python-script/5552/6 "2012-12-10T19:23:11Z")

</div>

Ah, I think I see the issue now. You’re trying to build the query string yourself and you need to know how to convert the date string. You can use java.sql.Timestamp which wraps the Java Date so that the JDBC API sees it as a SQL TIMESTAMP. Try this:

[code]from java.sql import Timestamp

s = event.source.parent.getComponent(‘Date Range’).startDate  
new\_s = Timestamp(s.getTime())  
q = “SELECT Machine FROM DataMachines WHERE ts\>=’%s’” %(new\_s)[/code]

---

<div class="post-metadata">

**Author:** ![Step7](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Step7](https://forum.inductiveautomation.com/u/Step7)\
**Post date:** [December 10, 2012, 8:13pm UTC](https://forum.inductiveautomation.com/t/passing-calendar-date-into-python-script/5552/7 "2012-12-10T20:13:12Z")

</div>

Yep, that did the trick. Thanks!
