Round down Timestamps to minutes

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

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

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

Test Flow:

[{"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":[]}]
1 Like

Or you can use Transform syntax in JSONata

$$.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

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

to make it back to timestamp ?
maybe with $toMillis() like this

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

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