# Script Coding Question (Import Datetime)

**URL:** <https://forum.inductiveautomation.com/t/script-coding-question-import-datetime/2049>\
**Category:** Ignition\
**Created:** [July 18, 2010, 3:47am UTC](https://forum.inductiveautomation.com/t/script-coding-question-import-datetime/2049 "2010-07-18T03:47:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RRNut](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@RRNut](https://forum.inductiveautomation.com/u/RRNut)\
**Post date:** [July 18, 2010, 3:47am UTC](https://forum.inductiveautomation.com/t/script-coding-question-import-datetime/2049/1 "2010-07-18T03:47:39Z")

</div>

I’m trying to create a script that runs every second to populate variables with various time information. Here is a snippet that I cannot get to work. I’m trying to use the Python Datetime module or if this module isn’t supported than similiar functionality.

Here is the code sample:

import datetime  
tYday = datetime.datetime.now().timetuple().tm\_yday

I believe the problem is with the import item.

The complete section of code is listed below. In the IDLE python editor it funcitons as normal.

[i]import datetime  
tYear = datetime.datetime.now().timetuple().tm\_year  
tMon = datetime.datetime.now().timetuple().tm\_mon  
tDay = datetime.datetime.now().timetuple().tm\_mday  
tHour = datetime.datetime.now().timetuple().tm\_hour  
tMin = datetime.datetime.now().timetuple().tm\_min  
tSec = datetime.datetime.now().timetuple().tm\_sec  
tYday = datetime.datetime.now().timetuple().tm\_yday

if tMin \>= 0 and tMin \< 15:  
if tHour%2 == 0:  
period = 1  
else:  
period = 5

if tMin \>= 15 and tMin \< 30:  
if tHour%2 == 0:  
period = 2  
else:  
period = 6

if tMin \>= 30 and tMin \< 45:  
if tHour%2 == 0:  
period = 3  
else:  
period = 7

if tMin \>= 45 and tMin \< 60:  
if tHour%2 == 0:  
period = 4  
else:  
period = 8

block = abs(round(((tHour/2)-.1),0))+1  
dayyear = tYear\*1000 + tYday

print period  
print dayyear

print tYear  
print tMon  
print tDay  
print tHour  
print tMin  
print tSec  
print tYday[/i]

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [July 18, 2010, 10:58am UTC](https://forum.inductiveautomation.com/t/script-coding-question-import-datetime/2049/2 "2010-07-18T10:58:49Z")

</div>

Have you tried using tags and dynamic properties? Bind a property to the following expression and you'll get a property that updates automatically (from events firing) without the worry of getting a script to run at the right times.

```auto
dateExtract({[System]Client/System/CurrentDateTime}, "minute")
```

You can then create other properties that are bound to such expressions to get all your desired values.

> [@RRNut](#):
>
> I'm trying to create a script that runs every second to populate variables with various time information.

---

<div class="post-metadata">

**Author:** ![RRNut](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@RRNut](https://forum.inductiveautomation.com/u/RRNut)\
**Post date:** [July 18, 2010, 4:19pm UTC](https://forum.inductiveautomation.com/t/script-coding-question-import-datetime/2049/3 "2010-07-18T16:19:46Z")

</div>

Nathan - Thanks for your help. I really need alot of the datetime, period, block, dayyear information updated continously. These values will be used to fire events for other activities.

I tried to use the [System]Client/System/CurrentDateTime in a 1 sec timer script and I cannot get it to work. I also notice that dateextract function doesn’t support day of year. When ever there is change in tags period or block I need to perform tasks.

Maybe I’m still approaching this problem incorrectly. I’m definitly open to suggestions. I’m familiar with the Python datetime library.

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [July 18, 2010, 7:34pm UTC](https://forum.inductiveautomation.com/t/script-coding-question-import-datetime/2049/4 "2010-07-18T19:34:54Z")

</div>

datetime doesn’t appear to be a supported module. [This post](http://www.inductiveautomation.com/forum/viewtopic.php?p=3760#p3760) lists supported python modules. It may be dated.

My first suggestion used Dynamic Properties. It’s a powerful tool, but probably not relevant since it would require that some window be open (you want this to run continually).

You could take a similar approach with “DB SQLTags”. You could derive your period and block values from SQL Queries, then run your scripts as “Gateway Tag Script Changes” on value changes.

Could you provide a higher level description of the things you want the script to do and when?

---

<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:** [July 19, 2010, 6:36pm UTC](https://forum.inductiveautomation.com/t/script-coding-question-import-datetime/2049/5 "2010-07-19T18:36:37Z")

</div>

The list of supported modules is in [the user manual](http://www.inductiveautomation.com/support/usermanuals/ignition/index.html?python_standard_library.htm). Datetime is not among them. Conveniently, Java has capable date handling. Here is a re-written version of your script that should work:

`from java.util import Calendar
now = Calendar.getInstance()
tYear = now.get(Calendar.YEAR)
tMon = now.get(Calendar.MONTH)
tDay = now.get(Calendar.DAY_OF_MONTH)
tHour = now.get(Calendar.HOUR)
tMin = now.get(Calendar.MINUTE)
tSec = now.get(Calendar.SECOND)
tYday = now.get(Calendar.DAY_OF_YEAR)`

Hope this helps,

---

<div class="post-metadata">

**Author:** ![RRNut](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@RRNut](https://forum.inductiveautomation.com/u/RRNut)\
**Post date:** [July 19, 2010, 6:42pm UTC](https://forum.inductiveautomation.com/t/script-coding-question-import-datetime/2049/6 "2010-07-19T18:42:47Z")

</div>

Thanks for your help guys!!! I appreciate the outstanding support that is provided in this forum!
