# How to change a Timestamp variable value without changing it to a string?

**URL:** <https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [December 6, 2023, 2:49am UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757 "2023-12-06T02:49:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![r123](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@r123](https://forum.inductiveautomation.com/u/r123)\
**Post date:** [December 6, 2023, 2:49am UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757/1 "2023-12-06T02:49:06Z")

</div>

A script that had been working for months now causes a function that is passed the start time and end time to no longer work. Only the end time has an error and it is created from the start time.

I was using the `system.date.addMinutes` function to create an end time variable from the start time.

`found_endTime = system.date.addMinutes(found_startTime, -10)`

The `found_startTime` variable is type 'java.sql.Timestamp' and the `found_endTime` is type 'unicode'.

If I pass two timestamps that are both type 'java.sql.Timestamp' the function works.

---

<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:** [December 6, 2023, 9:04am UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757/2 "2023-12-06T09:04:33Z")

</div>

> [@r123](#):
>
> The `found_endTime` variable is type 'java.sql.Timestamp' and the `found_endTime` is type 'unicode'.

I think you've mis-typed the variables in that sentence. Should the first one be `found_startTime`?

I was unable to reproduce the problem in Script Console on 8.1.31.

---

<div class="post-metadata">

**Author:** ![r123](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@r123](https://forum.inductiveautomation.com/u/r123)\
**Post date:** [December 6, 2023, 4:36pm UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757/3 "2023-12-06T16:36:57Z")

</div>

Sorry, that should have been the start `found_startTime` as type `java.sql.Timestamp` that works.

`found_startTime` is read using `system.db.runNamedQuery` and is a type `java.sql.Timestamp` and if I print the value it is something like "`2023-12-05 20:26:14.777`".

When I try to create the found\_endTime with the `system.date.addMinutes` function it becomes type 'java.util.Date', looks like this "`Tue Dec 05 20:16:14 CST 2023`" and stops the function it's passed to from working.

I missed that it only becomes type 'unicode' after trying to change the format to match with the `system.date.format` function. It then looks like this "2023-12-05 20:16:14.000" but doesn't work.

---

<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:** [December 6, 2023, 5:07pm UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757/4 "2023-12-06T17:07:08Z")

</div>

> [@r123](#):
>
> it becomes type 'java.util.Date'

That should be fine. That's just the human readable (but ugly) Ignition representation of the underlying date datatype. Don't format it until you need to at the last moment before display. Always leave them alone when passing from one function to another.

> [@r123](#):
>
> ... and stops the function it's passed to from working.

You'll have to share some information about that function. Post the code and format it correctly using the \</\> button.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [December 6, 2023, 5:08pm UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757/5 "2023-12-06T17:08:22Z")

</div>

> [@r123](#):
>
> and stops the function it's passed to from working.

I think you need to cough up the code for this mystery function you keep referencing.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 6, 2023, 5:51pm UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757/6 "2023-12-06T17:51:55Z")

</div>

> [@r123](#):
>
> `found_startTime` is read using `system.db.runNamedQuery` and is a type `java.sql.Timestamp` and if I print the value it is something like "`2023-12-05 20:26:14.777`".
> 
> When I try to create the found\_endTime with the `system.date.addMinutes` function it becomes type 'java.util.Date', looks like this "`Tue Dec 05 20:16:14 CST 2023`" and stops the function it's passed to from working.

Those are exactly the same thing. java.sql.Timestamp is a extension of the java.util.Date. When you "print" them out you get whatever representation has been chosen for that class. The information in the objects is the same.

If you function is dependent on a specific format, then you're, IMHO, doing it "wrong".

You should be using the date object up until you need it to be a string, and then do the format.

The "unicode" is just the encoding, the object is just a string.

What is failing in the function?

---

<div class="post-metadata">

**Author:** ![r123](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@r123](https://forum.inductiveautomation.com/u/r123)\
**Post date:** [December 7, 2023, 4:43pm UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757/7 "2023-12-07T16:43:26Z")

</div>

I don't know how it had been working before but I tried removing the format change step again and this time it now it works. I had tried that the other day, checking the type passed each time and seeing it still fail after each change until the type was `java.sql.Timestamp`. ???

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 7, 2023, 7:23pm UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757/8 "2023-12-07T19:23:07Z")

</div>

'format' returns a string. Which is not a date, only the representation of a date.  
date objects DO NOT have a format. Their representation does. What you see when you print a date is NOT the date, it's the string a call to their ` __repr__ ` or ` __str__ ` methods returns. They do pretty much what `format` does, except that you don't pick the format.  
This is why you can get wildly different representations for two very similar objects. They might contain the same date, but print very differently.

So in trying to make one object **look** like the other, you ended up changing the underlying data, and more importantly its type.

Which is pretty much what @lrose said above.

---

<div class="post-metadata">

**Author:** ![r123](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@r123](https://forum.inductiveautomation.com/u/r123)\
**Post date:** [December 7, 2023, 8:19pm UTC](https://forum.inductiveautomation.com/t/how-to-change-a-timestamp-variable-value-without-changing-it-to-a-string/82757/9 "2023-12-07T20:19:26Z")

</div>

> [@pascal.fragnoud](#):
>
> This is why you can get wildly different representations for two very similar objects. They might contain the same date, but print very differently.

That is why I was using the format. In the end it must be in that format and it was working for some time. It seems it should have never worked, passing the date after it was formatted. Then it was still not working after I thought I'd removed the formatting step, so I was a bit lost. I must have changed something at some point, I just don't see how or when. Thanks for the info.
