# Datetime.now() in transform script returning wrong time

**URL:** <https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [November 11, 2021, 2:27pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962 "2021-11-11T14:27:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![henning.e.heine](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/henning.e.heine/32/70958_2.png) [@henning.e.heine](https://forum.inductiveautomation.com/u/henning.e.heine)\
**Post date:** [November 11, 2021, 2:27pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/1 "2021-11-11T14:27:29Z")

</div>

I am using datetime.now() in a transform script to make some transformations based on the current time.  
In the script the returned time is off by 1 hour.

datetime.now() in script console returns: 2021-11-11 15:25:400  
datetime.now() in transform script returns: 2021-11-11 16:25:00

Does this have to to with daylight saving time or some local time settings? I am based in Germany.

---

<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:** [November 11, 2021, 2:28pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/2 "2021-11-11T14:28:36Z")

</div>

Hmm, well one difference is that in the transform is running on your gateway but the script console is running on the PC you launched the Designer on.

---

<div class="post-metadata">

**Author:** ![henning.e.heine](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/henning.e.heine/32/70958_2.png) [@henning.e.heine](https://forum.inductiveautomation.com/u/henning.e.heine)\
**Post date:** [November 11, 2021, 2:49pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/3 "2021-11-11T14:49:07Z")

</div>

Thanks for your response!  
I just checked Gateway time and PC time are in sync.

…

---

<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:** [November 11, 2021, 5:35pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/4 "2021-11-11T17:35:01Z")

</div>

Check the `[System]Gateway/CurrentDateTime` tag specifically; the Gateway JVM itself could have a different time than the underlying system (for a variety of reasons).

---

<div class="post-metadata">

**Author:** ![henning.e.heine](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/henning.e.heine/32/70958_2.png) [@henning.e.heine](https://forum.inductiveautomation.com/u/henning.e.heine)\
**Post date:** [November 12, 2021, 11:58am UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/5 "2021-11-12T11:58:59Z")

</div>

Thanks for that hint. But now I am only more confused.  
[System]Gateway/CurrentDateTime shows the same time as my PC.In the property browser the time is still correct, but as soon as I visualize the time on a view the time is off by one hour.

Please have a look at the screenshots:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/c/acdf6df522d2e30484a5341f1df38157c4f9bfb1.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/3/e35a729adb9ab88549d890c8fa04b828cfac9293.png)

---

<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:** [November 12, 2021, 12:08pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/6 "2021-11-12T12:08:40Z")

</div>

The ‘Z’ at the end refers to ‘Zero offset hour’, or UTC (Or Zulu time, if you’ve ever heard the phrase).

[ISO 8601 Standard](https://en.wikipedia.org/wiki/ISO_8601)

```python
t = system.date.now()

print t
print t.toInstant()

```

Output:

```nohighlight
Fri Nov 12 07:11:18 EST 2021
2021-11-12T12:11:18.656Z

```

---

<div class="post-metadata">

**Author:** ![henning.e.heine](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/henning.e.heine/32/70958_2.png) [@henning.e.heine](https://forum.inductiveautomation.com/u/henning.e.heine)\
**Post date:** [November 12, 2021, 12:54pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/7 "2021-11-12T12:54:10Z")

</div>

I can see the difference. But how can I know what time is actualy being used.  
In the text propery the time is shown without formatnig it to Zulu time. On the view it suddenly is displayed as Zulu time.

And why is the Zulu time differnt if I use a transform (which should be running on the Gatway) and when I use a tag binding to [System]Gateway/CurrentDateTime.

edit: And my problem was not the Zulu time, I think. my Problem is that my time that is used in a Script- Transform with datetime.now() adds 1 hour to the actual local time.

edit2: I guess for this specific case I could switch from datetime.now() to system.date.now(). But I would really like to understand why both methodes return different times.

---

<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:** [November 12, 2021, 1:27pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/8 "2021-11-12T13:27:45Z")

</div>

It’s because datetime is not time zone aware. It’s also as you have found, a huge pain to use. I recommend using java date types whenever possible.

---

<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:** [November 12, 2021, 1:58pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/9 "2021-11-12T13:58:14Z")

</div>

> [@JordanCClark](#):
>
> I recommend using java date types whenever possible.

💯

---

<div class="post-metadata">

**Author:** ![ShaneH](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@ShaneH](https://forum.inductiveautomation.com/u/ShaneH)\
**Post date:** [November 12, 2021, 6:14pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/10 "2021-11-12T18:14:55Z")

</div>

How do you force the use of Java date types from an Ignition Python script?

---

<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:** [November 12, 2021, 6:22pm UTC](https://forum.inductiveautomation.com/t/datetime-now-in-transform-script-returning-wrong-time/52962/11 "2021-11-12T18:22:24Z")

</div>

Ignore the existence of `datetime`. Use `system.date` functions, or native java types directly (e.g. [`Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html) for more complex operations than `system.date` provides).

If you’re using a library that returns datetime types, avoid it or refactor it or convert to java types as soon as possible.
