# System.date.now Timezone

**URL:** <https://forum.inductiveautomation.com/t/system-date-now-timezone/72478>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [April 14, 2023, 9:12pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478 "2023-04-14T21:12:47Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob.ramirez](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jacob.ramirez](https://forum.inductiveautomation.com/u/jacob.ramirez)\
**Post date:** [April 14, 2023, 9:12pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/1 "2023-04-14T21:12:47Z")

</div>

When running the function system.date.now() from a perspective session, what time zone should be returned? The user's computer time? Or the server's time?

---

<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:** [April 14, 2023, 9:42pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/2 "2023-04-14T21:42:38Z")

</div>

> [@jacob.ramirez](#):
>
> system.date.now()

In Perspective all scripts are run on the gateway so it will return the server's time.

---

<div class="post-metadata">

**Author:** ![jacob.ramirez](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jacob.ramirez](https://forum.inductiveautomation.com/u/jacob.ramirez)\
**Post date:** [April 14, 2023, 9:49pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/3 "2023-04-14T21:49:16Z")

</div>

When I go to my Gateway Status page, it shows the time which is in mountain time.

When I run the system.date.now() off a button script in a perspective session, it returns UTC time. Why won't it return the servers time which is in Mountain time?

---

<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:** [April 14, 2023, 9:57pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/4 "2023-04-14T21:57:40Z")

</div>

Where are you seeing the UTC? In your Perspective client?

Go to Project | Project Properties | Perspective General and check what the Project Timezone setting is.

---

<div class="post-metadata">

**Author:** ![jacob.ramirez](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jacob.ramirez](https://forum.inductiveautomation.com/u/jacob.ramirez)\
**Post date:** [April 14, 2023, 10:01pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/5 "2023-04-14T22:01:45Z")

</div>

I'm seeing UTC from the perspective client.

The project timezone in the project properties is set to 'Client Timezone'

---

<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:** [April 14, 2023, 10:09pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/6 "2023-04-14T22:09:35Z")

</div>

Hmmm. I'm not sure then. (I'm in Ireland and running on GMT so it's not a problem I have to deal with much.) Someone will be along ...

One last check: When you say "client" do you mean Designer or browser? I suspect they may react differently.

---

<div class="post-metadata">

**Author:** ![jacob.ramirez](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jacob.ramirez](https://forum.inductiveautomation.com/u/jacob.ramirez)\
**Post date:** [April 14, 2023, 10:13pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/7 "2023-04-14T22:13:46Z")

</div>

Browser. I tested calling this function running a perspective session via a browser. I'm getting strange behavior when data is inserted into the database (using only system.date.now and system.date.getTimezoneOffset, not any database now functions) from a client in the central timezone.

Our server is in the mountain timezone, I store timestamps in the database in 4 columns: UTC, CST, MST, and EST. I am now getting odd values in the database from users in the central time zone, which we just recently started having users in the central time zone.

---

<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 14, 2023, 10:25pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/8 "2023-04-14T22:25:38Z")

</div>

The `java.util.Date()` object returned from any of Ignition's date and time functions, whether expression or script, **Has No TimeZone!** It is **always** UTC milliseconds. It is converted to a zoned format when **converted to string**.

So, if converted to a string in the gateway, it will use the gateway timezone. If in Perspective, **in the browser** , it will use Perspective's client timezone.

If you convert to string yourself, in a script or expression in Perspective, since those run in the gateway, you will get the gateway's timezone.

---

<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 14, 2023, 10:28pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/9 "2023-04-14T22:28:11Z")

</div>

> [@jacob.ramirez](#):
>
> I'm getting strange behavior when data is inserted into the database

MS SQL Server? Its datetime column doesn't play nicely with `java.util.Date`, and converts to/from a localtime format using the gateway to DB connection properties, which is typically the gateway timezone.

To get reliable Ignition datetime values with MS SQL Server, run both servers in UTC.

---

<div class="post-metadata">

**Author:** ![jacob.ramirez](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jacob.ramirez](https://forum.inductiveautomation.com/u/jacob.ramirez)\
**Post date:** [April 15, 2023, 1:10am UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/10 "2023-04-15T01:10:43Z")

</div>

This is making more sense now. I'm using MySQL but for timestamps, I'm passing all timestamps (UTC, CST, MST, EST) in as parameters.

Below is the function that I'm using to get all 4 timestamps. I use this mostly in gateway scripts but recently started using this function from a Perspective client for data entry to a database.

Basically, I'm getting the UTC offset based on system.date.now, then getting the UTC time from that offset (assuming that the system.date.now() is not in UTC when called). I then assumed that system.date.getTimezone() would change and that's why I have the if statements at the bottom.

```auto
def get_timestamps(utc_timestamp=None):
    timezone = system.date.getTimezone()
    utc_offset = system.date.getTimezoneOffset(system.date.now())
    timestamps = {}
    if utc_timestamp is None:
        timestamps["utc_timestamp"] = system.date.addHours(
            system.date.now(), int(abs(utc_offset))
        )
    else:
        timestamps["utc_timestamp"] = utc_timestamp
    mst_offset = 0
    cst_offset = 0
    est_offset = 0
    if timezone == "America/Chicago":
        mst_offset = utc_offset - 1
        cst_offset = utc_offset
        est_offset = utc_offset + 1
    else:
        mst_offset = utc_offset
        cst_offset = utc_offset + 1
        est_offset = utc_offset + 2
    timestamps["mst_timestamp"] = system.date.addHours(
        timestamps["utc_timestamp"], int(mst_offset)
    )
    timestamps["cst_timestamp"] = system.date.addHours(
        timestamps["utc_timestamp"], int(cst_offset)
    )
    timestamps["est_timestamp"] = system.date.addHours(
        timestamps["utc_timestamp"], int(est_offset)
    )
    return timestamps

```

---

<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 15, 2023, 1:45am UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/11 "2023-04-15T01:45:43Z")

</div>

Don't do that. One instant in time, regardless of zone, is exactly and correctly represented by a `java.util.Date` object. If you serialize it and send to another machine, it **displays** in that machine's timezone, but is still the same point in real time.

---

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [October 17, 2023, 11:59am UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/12 "2023-10-17T11:59:01Z")

</div>

Would it not make sense to get a system.date.nowUtc() instead of using java.util.date? I need a convenient way to use utc dates in my scripts.

---

<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:** [October 17, 2023, 12:27pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/13 "2023-10-17T12:27:45Z")

</div>

But `system.date.now()` _ **IS** _ UTC. If you need to convert strings to/from UTC everywhere in your gateway, set your gateway timezone to UTC. If you need to use a variety of time zones in gateway scripts (and Perspective scripts), you will need to ask java to render in a specific time zone instead of the gateway timezone. Some techniques:

> [@Ignition Perspective Page to show the client System date and time](https://forum.inductiveautomation.com/t/ignition-perspective-page-to-show-the-client-system-date-and-time/73265/8):
>
> Sure. I recommend [DateTimeFormatter.ofPattern()](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html#ofPattern(java.lang.String)). But I would move the complexity to a project script module with a DateTimeFormatter instance with your preferred pattern saved as a top level variable. Like so, perhaps: from java.time import ZoneId from java.time.format import DateTimeFormatter defaultFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm") def zonedFormat(ts, zoneid, format=None): formatter = defaultFormatter if format is None else DateTimeFormatter.ofPattern(format) r…

Let me emphasize what I wrote in an earlier comment: `java.util.Date` _ **objects** _ are UTC milliseconds under the hood. They already _ **are** _ UTC. Java converts them to strings for **display** in the JVM's timezone. When a `java.util.Date` object is serialized and transmitted to a client or designer, it travels **as UTC**. And then gets stringified in a local time zone wherever the stringification happens.

---

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [October 17, 2023, 12:34pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/14 "2023-10-17T12:34:34Z")

</div>

Oh okay! So if IT refuses to put the server in UTC, we will always have time zone based dates in Ignition?

---

<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:** [October 17, 2023, 12:49pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/15 "2023-10-17T12:49:06Z")

</div>

You will always have timezone-based date **strings** in Ignition. The dates themselves (the **objects** ) have no time zone.

---

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [October 17, 2023, 12:59pm UTC](https://forum.inductiveautomation.com/t/system-date-now-timezone/72478/16 "2023-10-17T12:59:54Z")

</div>

Ok, yes, got it.
