# Separate timestamp

**URL:** <https://discourse.nodered.org/t/separate-timestamp/44377>\
**Category:** General\
**Created:** [19 April 2021 05:39 UTC](https://discourse.nodered.org/t/separate-timestamp/44377 "2021-04-19T05:39:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lehoangtrung](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@lehoangtrung](https://discourse.nodered.org/u/lehoangtrung)\
**Post date:** [19 April 2021 05:39 UTC](https://discourse.nodered.org/t/separate-timestamp/44377/1 "2021-04-19T05:39:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [19 April 2021 06:50 UTC](https://discourse.nodered.org/t/separate-timestamp/44377/2 "2021-04-19T06:50:13Z")

</div>

> [@Get time matching ui\_date\_picker](https://discourse.nodered.org/t/get-time-matching-ui-date-picker/42601/3):
>
> I use [node-red-contrib-moment](https://flows.nodered.org/node/node-red-contrib-moment) here is an example.... [{"id":"38408627.2b4cda","type":"moment","z":"150b582b.3e6d68","name":"year","topic":"","input":"epoch","inputType":"msg","inTz":"America/New\_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY","locale":"C","output":"year","outputType":"msg","outTz":"America/New\_York","x":330,"y":860,"wires":[["16c64f42.d8cb39"]]},{"id":"16c64f42.d8cb39","type":"moment","z":"150b582b.3e6d68","name":"mon…

---

<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:** [19 April 2021 07:14 UTC](https://discourse.nodered.org/t/separate-timestamp/44377/3 "2021-04-19T07:14:10Z")

</div>

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.

```auto
[{"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":[]}]

```

```auto
(
   $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

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

```

---

<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:** [18 June 2021 07:14 UTC](https://discourse.nodered.org/t/separate-timestamp/44377/4 "2021-06-18T07:14:36Z")

</div>

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