# Getting the "floor" of a timestamp and writing this to a tag

**URL:** <https://forum.inductiveautomation.com/t/getting-the-floor-of-a-timestamp-and-writing-this-to-a-tag/85708>\
**Category:** Ignition\
**Created:** [February 14, 2024, 6:43pm UTC](https://forum.inductiveautomation.com/t/getting-the-floor-of-a-timestamp-and-writing-this-to-a-tag/85708 "2024-02-14T18:43:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [February 14, 2024, 6:43pm UTC](https://forum.inductiveautomation.com/t/getting-the-floor-of-a-timestamp-and-writing-this-to-a-tag/85708/1 "2024-02-14T18:43:09Z")

</div>

Suppose I have a script that, when triggered, writes system.date.now() to a memory tag. It always fires roughly around when a shift begins (this time/day varies constantly based on our production schedule). This memory tag's value can end up being any timestamp from (using first shift today as an example) 2024-02-14 7:00:00 AM up to 7:10:00 AM (or sometimes taking even longer). It will never fire early (e.g. system.date.now() in this context will never output 6:59:XX values or below).

I'm wondering if someone can help me modify the script such that the system.date.now() timestamp rounds down to the nearest hour before the tag write occurs, if that makes sense? I reviewed the [system.date functions](https://docs.inductiveautomation.com/display/DOC81/system.date) and can conceive of a couple ways to do this by converting from java, parsing a string, and so on, but anything I come up with gets convoluted pretty quickly. I'm basically in search of an equivalent floor() function but for timestamps. The transforms would specifically look like this:

```auto
now() [Input] Output
 2024-02-14 7:07:39 AM 2024-02-14 7:00:00 AM
 2024-02-14 2:01:15 PM 2024-02-14 2:00:00 PM
 2024-02-15 1:00:45 AM 2024-02-15 1:00:00 AM
2024-02-15 12:30:02 AM 2024-02-15 12:00:00 AM

```

Any ideas?

---

<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:** [February 14, 2024, 6:49pm UTC](https://forum.inductiveautomation.com/t/getting-the-floor-of-a-timestamp-and-writing-this-to-a-tag/85708/2 "2024-02-14T18:49:01Z")

</div>

Hmm. Maybe something like:

```auto
 def round_nearest_hour(timestamp):
    time = timestamp.getTime()
    time = time - (time % 3600)
    return java.util.Date(time)

```

(untested)

---

<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:** [February 14, 2024, 6:57pm UTC](https://forum.inductiveautomation.com/t/getting-the-floor-of-a-timestamp-and-writing-this-to-a-tag/85708/3 "2024-02-14T18:57:52Z")

</div>

I would probably just:

`system.date.setTime(date, system.date.getHour24(date), 0, 0)`

Which you could also write out as an expression, for performance.

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [February 14, 2024, 8:12pm UTC](https://forum.inductiveautomation.com/t/getting-the-floor-of-a-timestamp-and-writing-this-to-a-tag/85708/4 "2024-02-14T20:12:24Z")

</div>

system.date.setTime() is the magic function I needed! I missed this when looking through the docs. Works great now, thank you!!
