# Getting a datetime to survive a JSON round trip to the database

**URL:** <https://forum.inductiveautomation.com/t/getting-a-datetime-to-survive-a-json-round-trip-to-the-database/81185>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [October 30, 2023, 6:08pm UTC](https://forum.inductiveautomation.com/t/getting-a-datetime-to-survive-a-json-round-trip-to-the-database/81185 "2023-10-30T18:08:33Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [October 30, 2023, 6:08pm UTC](https://forum.inductiveautomation.com/t/getting-a-datetime-to-survive-a-json-round-trip-to-the-database/81185/1 "2023-10-30T18:08:33Z")

</div>

- I'm storing a date as part of a schedule in a configuration database table and the field is a MySQL JSON type. It works well except ...
- `system.util.jsonEncode()` results in the date being saved as a JSON string in format `Oct 5, 2023, 12:00:00 AM`.
- When read back and run through `system.util.json.Decode()` it comes out as a string rather than a date.

I notice on [json.org](https://json.org) that there is no JSON date type.

Is there a simple fix for this or should I store as `millis`?

---

<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:** [October 30, 2023, 6:10pm UTC](https://forum.inductiveautomation.com/t/getting-a-datetime-to-survive-a-json-round-trip-to-the-database/81185/2 "2023-10-30T18:10:02Z")

</div>

Whether you store it as millis or leave it as a String, you'll still need some additional step to turn that into a Date at some point, won't you?

---

<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:** [October 30, 2023, 6:11pm UTC](https://forum.inductiveautomation.com/t/getting-a-datetime-to-survive-a-json-round-trip-to-the-database/81185/3 "2023-10-30T18:11:32Z")

</div>

Anyway, I'd store it as millis since epoch UTC, to avoid any timezone/formatting/parsing issues.

If you were using a String you'd want it to be ISO 8601 UTC but that doesn't seem to be an option given whatever `system.util.jsonEncode` is doing.

---

<div class="post-metadata">

**Author:** ![humphrey.daniel](https://avatars.discourse-cdn.com/v4/letter/h/8e8cbc/32.png) [@humphrey.daniel](https://forum.inductiveautomation.com/u/humphrey.daniel)\
**Post date:** [October 30, 2023, 6:13pm UTC](https://forum.inductiveautomation.com/t/getting-a-datetime-to-survive-a-json-round-trip-to-the-database/81185/4 "2023-10-30T18:13:23Z")

</div>

You might benefit from making this an object with value and type properties so you know how to transform it after decoding. Type info is lost once stored in JSON as a string.
