# Best way to construct timestamp from elements

**URL:** <https://forum.inductiveautomation.com/t/best-way-to-construct-timestamp-from-elements/11919>\
**Category:** Ignition\
**Created:** [July 12, 2016, 2:21pm UTC](https://forum.inductiveautomation.com/t/best-way-to-construct-timestamp-from-elements/11919 "2016-07-12T14:21:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![george](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/george/32/14066_2.png) [@george](https://forum.inductiveautomation.com/u/george)\
**Post date:** [July 12, 2016, 2:21pm UTC](https://forum.inductiveautomation.com/t/best-way-to-construct-timestamp-from-elements/11919/1 "2016-07-12T14:21:08Z")

</div>

I want to construct a timestamp from individual elements (year, month, day, hour, minute, second).  
The elements are initially coming from a PLC and then I’m writing into a database.

This script (admittedly with some hard-coded values) appears to work:

[code]from datetime import datetime  
dt\_y = 2016  
dt\_m = 1  
dt\_d = 20  
dt\_h = 12  
dt\_mn = 34  
dt\_s = 56  
tstamp=datetime(dt\_y, dt\_m, dt\_d, dt\_h, dt\_mn, dt\_s)

sql = “UPDATE mytable SET t\_stamp=? WHERE ID=1”  
system.db.runPrepUpdate(sql,[tstamp])[/code]

But is this the best way of doing it?

---

<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:** [July 12, 2016, 2:45pm UTC](https://forum.inductiveautomation.com/t/best-way-to-construct-timestamp-from-elements/11919/2 "2016-07-12T14:45:46Z")

</div>

There’s probably not a “right” answer to this. What you have is certainly acceptable. Since Ignition is built with java and jython has to convert to java objects to move data through the system, I tend to use the corresponding java objects directly. There are various differences, but the same end effect. Somewhat more efficient if needed in big loops.[code]from java.util import Date  
dt\_y = 2016  
dt\_m = 1  
dt\_d = 20  
dt\_h = 12  
dt\_mn = 34  
dt\_s = 56  
tstamp=Date(dt\_y-1900, dt\_m-1, dt\_d, dt\_h, dt\_mn, dt\_s)

sql = “UPDATE mytable SET t\_stamp=? WHERE ID=1”  
system.db.runPrepUpdate(sql,[tstamp])[/code]
