# Convert a float to a datetime

**URL:** <https://forum.inductiveautomation.com/t/convert-a-float-to-a-datetime/35158>\
**Category:** Ignition\
**Created:** [May 29, 2020, 1:50pm UTC](https://forum.inductiveautomation.com/t/convert-a-float-to-a-datetime/35158 "2020-05-29T13:50:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [May 29, 2020, 1:50pm UTC](https://forum.inductiveautomation.com/t/convert-a-float-to-a-datetime/35158/1 "2020-05-29T13:50:11Z")

</div>

Good morning,

I have a float value (ex 5.444454).  
How would I convert this float into CURRENT\_TIMESTAMP + MM:SS?

Preferably I would like to accomplish this inside my script library. The trickiest part seems to be converting the float to MM:SS. Adding it to CURRENT\_TIMESTAMP is just an afterthought.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 29, 2020, 1:52pm UTC](https://forum.inductiveautomation.com/t/convert-a-float-to-a-datetime/35158/2 "2020-05-29T13:52:13Z")

</div>

What date would 5.44454 represent to you? Just looking at it its not obvious. It sounds like you will definitely need a custom mapping function from float to timestamp.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [May 29, 2020, 1:54pm UTC](https://forum.inductiveautomation.com/t/convert-a-float-to-a-datetime/35158/3 "2020-05-29T13:54:20Z")

</div>

So 5.44454 in this example would be MM:SS of 05:28.  
So CURRENT\_TIMESTAMP + 05:28 would be May 29 2020 08:59:28 (or close)

FYI this new DATETIME value will be inserted into a SQL database (the end goal)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 29, 2020, 2:01pm UTC](https://forum.inductiveautomation.com/t/convert-a-float-to-a-datetime/35158/4 "2020-05-29T14:01:32Z")

</div>

Ah I see I think you mean then `May 29 2020 08:05:28` since 5.44454 = 5:28 right?

Are you retrofitting data or is this for a new table? In MySQL doing `CURRENT_TIMESTAMP()` saves the minute and seconds by default so theres no need to do this. I think all databases have some timestamp-esque function that saves the current minute and second.

Otherwise, if you are retrofitting data, sounds like you will need an update function where you do something like

```auto
ALTER myTable
ADD COLUMN myNewDate datetime;

UPDATE myTable
SET myNewData = DATEFORMAT(some logic here)

```

That dateformat part will change depending on what database you use, and will give you the opporutnity to say I want the year to be the year dictated by this column, the month from this column, and the minute/seconds based on calculations on this column.

But again, if this is a new table, CURRENT\_TIMESTAMP should already take care of minutes and seconds for you.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [May 29, 2020, 2:08pm UTC](https://forum.inductiveautomation.com/t/convert-a-float-to-a-datetime/35158/5 "2020-05-29T14:08:09Z")

</div>

So my client is reading a FLOAT value from PLC. A tag change script is activated at some time and the FLOAT is converted to MM:SS and then added to whatever CURRENT\_TIMESTAMP is.

Finally, this future timestamp is displayed on a screen for the user.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 29, 2020, 2:18pm UTC](https://forum.inductiveautomation.com/t/convert-a-float-to-a-datetime/35158/6 "2020-05-29T14:18:03Z")

</div>

Oh ok so this is strictly for displaying values on screen form a plc? That makes it a bit easier.

You can do this to get the current date in an expression  
`dateFormat(NOW(), "yyyy-MM-dd")` this will give you `2020-05-29`, but theres ways you can make it say the month if you want, look at the documentation.

Then just concatenate this with an expression that does your calculation to convert your float from minutes and seconds. So overall

```auto
dateFormat(NOW(), "yyyy-MM-dd") +
# some logic here to convert PLC float to minutes and seconds

```

You may have to do a `toStr()` around both date format and your expression, but don’t quote me on that.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 29, 2020, 6:19pm UTC](https://forum.inductiveautomation.com/t/convert-a-float-to-a-datetime/35158/7 "2020-05-29T18:19:19Z")

</div>

Since you’re asking to put it in a table, I’m assuming an insert. You didn’t mention which database, but this should work with most:

```python
time = 5.444454

query = "INSERT INTO myTable (t_stamp, future_t_stamp) VALUES (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + ( ? * INTERVAL '1 min')"

system.db.runPrepUpdate(query, [time])

```
