# Round down Timestamps to minutes

**URL:** <https://discourse.nodered.org/t/round-down-timestamps-to-minutes/64590>\
**Category:** General\
**Tags:** function-node\
**Created:** [1 July 2022 15:58 UTC](https://discourse.nodered.org/t/round-down-timestamps-to-minutes/64590 "2022-07-01T15:58:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DemalCal96](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@DemalCal96](https://discourse.nodered.org/u/DemalCal96)\
**Post date:** [1 July 2022 15:58 UTC](https://discourse.nodered.org/t/round-down-timestamps-to-minutes/64590/1 "2022-07-01T15:58:06Z")

</div>

Hello everyone. I have an array of objects with the following structure:

[{timestamp:1315485284895, value: 45},{timestamp:1315485285583, value: 49},{timestamp:1315485153214, value: 38},.....]

It has around 2000 objects. What I need is an easy way to round down all the timestamps to minutes, I mean, if the timestamp represents: 2022-06-30 16:52:23:86, I have to change it to: 2022-06-30 16:52:00:00

This is because I need to put all my data in a table with other measurements and I have to put values with same timestamp in the same row.

I would prefer to use a change node with a JSONata expression if possible

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [1 July 2022 16:07 UTC](https://discourse.nodered.org/t/round-down-timestamps-to-minutes/64590/2 "2022-07-01T16:07:12Z")

</div>

Use a Change node with a jsonata expression using the $moment library to zero out the seconds

```javascript
payload.{
    "timestamp": $moment(timestamp).format("YYYY-MM-DD HH:mm:00"),
    "value" : value
}

```

**Test Flow:**

```auto
[{"id":"24acf14596ed57a9","type":"inject","z":"54efb553244c241f","name":"data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"timestamp\":1315485284895,\"value\":45},{\"timestamp\":1315485285583,\"value\":49},{\"timestamp\":1315485153214,\"value\":38}]","payloadType":"json","x":330,"y":1600,"wires":[["91a9dc88fcf596c3"]]},{"id":"91a9dc88fcf596c3","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.{\t \"timestamp\": $moment(timestamp).format(\"YYYY-MM-DD HH:mm:00\"),\t \"value\" : value\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":1600,"wires":[["ac4a537168c541ee"]]},{"id":"ac4a537168c541ee","type":"debug","z":"54efb553244c241f","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1600,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 July 2022 16:20 UTC](https://discourse.nodered.org/t/round-down-timestamps-to-minutes/64590/3 "2022-07-01T16:20:59Z")

</div>

Or you can use Transform syntax in JSONata

```auto
$$.payload ~> |*|{"timestamp": $moment(timestamp).format("YYYY-MM-DD HH:mm:00.000")}|

```

which might be more preferable if each object had more properties. [Other Operators · JSONata](https://docs.jsonata.org/other-operators#-------transform)

---

<div class="post-metadata">

**Author:** ![DemalCal96](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@DemalCal96](https://discourse.nodered.org/u/DemalCal96)\
**Post date:** [1 July 2022 19:23 UTC](https://discourse.nodered.org/t/round-down-timestamps-to-minutes/64590/4 "2022-07-01T19:23:31Z")

</div>

Thank you. How can I change seconds and miliseconds, keeping the timestamp format?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [1 July 2022 19:49 UTC](https://discourse.nodered.org/t/round-down-timestamps-to-minutes/64590/5 "2022-07-01T19:49:45Z")

</div>

> [@DemalCal96](#):
>
> keeping the timestamp format?

to make it back to timestamp ?  
maybe with [$toMillis()](https://docs.jsonata.org/date-time-functions#tomillis) like this

```javascript
payload.{
    "timestamp": $toMillis(($moment(timestamp).format("YYYY-MM-DD HH:mm:00")), "[Y]-[M]-[D] [H]:[m]:[s]"),
    "value" : value
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 July 2022 19:50 UTC](https://discourse.nodered.org/t/round-down-timestamps-to-minutes/64590/6 "2022-07-15T19:50:11Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
