Separate timestamp

Hello,
I am building an IoT project. I need timing query from Timestamp.
Ex: Timestamp -> 1618809944697
So, how can i separate this Timestamp into year, month, day, hour, ...
help me please.

I prefer to use moments and JSONata to play with time , I just find it easier than JS.
So I would convert the time stamp, split the string and then convert to numbers.
Here is an example using a change node and JSONata and moments.

[{"id":"a80dbfdf.997f18","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":920,"wires":[["8315a549.29ab2"]]},{"id":"8315a549.29ab2","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"($dateparts := $split($moment(payload).tz(\"Europe/London\").format(\"YYYY-M-D-H-m-s\"),\"-\");\t$names := [\"year\",\"month\",\"day\",\"hours\",\"minutes\",\"seconds\"];\t$merge($map($dateparts, function($v,$i){{$names[$i]:$number($v)}}))\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":920,"wires":[["cbd69445.e1aaa8"]]},{"id":"cbd69445.e1aaa8","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":920,"wires":[]}]
(
   $dateparts := $split(
       $moment(payload).tz("Europe/London").format("YYYY-M-D-H-m-s"),
       "-"
   );
   $names := [
       "year",
       "month",
       "day",
       "hours",
       "minutes",
       "seconds"
   ];
   $merge(
       $map(
           $dateparts,
           function($v,$i){{$names[$i]:$number($v)}}
       )
   )
)

output

{"year":2021,"month":4,"day":19,"hours":8,"minutes":10,"seconds":12}
1 Like

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