# Dealing with Dates, Times, & Timestamps are never easy!

**URL:** <https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781>\
**Category:** Ignition\
**Tags:** database, ignition80, scripting\
**Created:** [April 19, 2021, 9:37pm UTC](https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781 "2021-04-19T21:37:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MikeAllgood](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@MikeAllgood](https://forum.inductiveautomation.com/u/MikeAllgood)\
**Post date:** [April 19, 2021, 9:37pm UTC](https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781/1 "2021-04-19T21:37:53Z")

</div>

I have need to compare a value from a Datetime field from SQL to the current time in a script.  
Unfortunately the value returned from my Name Query for the datetime value is of type java.sql.timestamp. When I try using the value in a system.date function I get an error like “1st arg can’t be coerced to java.util.Date”.  
When I mocked this up in the Script Console I did not get these errors. Once I put it in a Project Library Script and called that script from the Script Console, I started getting the errors.

So my questions are multi-faceted.

1. I know there are scope differences between Script Console and the Project Script Library, but why would system date function be concerned about scope? Or, is there another reason this works in the console but not in the project script?

2. How do I handle converting the java.sql.timestamp to a java.util.date, which is the data type the system date functions are expecting?

Is there something else I’m missing here that isn’t jumping out at me?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 19, 2021, 9:58pm UTC](https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781/2 "2021-04-19T21:58:03Z")

</div>

`java.sql.Timestamp` is a direct subclass of `java.util.Date` - do you have exact reproduction steps where you can’t pass one, but can pass the other? The way Java’s type restrictions work, anything that accepts a Date should automatically accept a Timestamp.

---

<div class="post-metadata">

**Author:** ![Paullys50](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paullys50/32/3324_2.png) [@Paullys50](https://forum.inductiveautomation.com/u/Paullys50)\
**Post date:** [April 19, 2021, 11:28pm UTC](https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781/3 "2021-04-19T23:28:27Z")

</div>

> [@MikeAllgood](#):
>
> How do I handle converting the java.sql.timestamp to a java.util.date, which is the data type the system date functions are expecting?

This is a common issue, send it through system.date.fromMillis(system.date.toMillis(sqltimestamp)) and it should convert to java.util.date type.

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [April 20, 2021, 1:24am UTC](https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781/4 "2021-04-20T01:24:30Z")

</div>

You can also convert it in your named query. I had a similar issue and it was easiest for me to use this in my query (MYSQL). Sql does not have From\_UnixTime() but there’s a way to do it.  
From\_UnixTime(sqlth\_1\_data.t\_stamp / 1000)

---

<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:** [April 20, 2021, 10:29am UTC](https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781/5 "2021-04-20T10:29:10Z")

</div>

```python
from java.util import Date
# Return a java.util.date from java.sql.Timestamp
date = Date(sqltimestamp.getTime())

```

@paul-griffith, from the Javadocs:

```nohighlight
Due to the differences between the `Timestamp` class and the `java.util.Date` class mentioned above,
it is recommended that code not view `Timestamp` values generically as an instance of `java.util.Date` . 
The inheritance relationship between `Timestamp` and `java.util.Date` really denotes 
implementation inheritance, and not type inheritance.

```

---

<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:** [April 20, 2021, 11:38am UTC](https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781/6 "2021-04-20T11:38:24Z")

</div>

I prefer java.sql.Timestamp over simple java.util.Date, as it can hold the microsecond timestamps from Logix processors. (:

---

<div class="post-metadata">

**Author:** ![MikeAllgood](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@MikeAllgood](https://forum.inductiveautomation.com/u/MikeAllgood)\
**Post date:** [April 20, 2021, 1:49pm UTC](https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781/7 "2021-04-20T13:49:46Z")

</div>

Thanks All!  
Your help was, well very HELPFUL! 😃

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [October 24, 2022, 6:49pm UTC](https://forum.inductiveautomation.com/t/dealing-with-dates-times-timestamps-are-never-easy/45781/8 "2022-10-24T18:49:27Z")

</div>

How did you convert them to java.sql.Timestamp ?
