# Time parsing - how to cut up time messages

**URL:** <https://discourse.nodered.org/t/time-parsing-how-to-cut-up-time-messages/19485>\
**Category:** General\
**Created:** [22 December 2019 23:37 UTC](https://discourse.nodered.org/t/time-parsing-how-to-cut-up-time-messages/19485 "2019-12-22T23:37:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 December 2019 23:37 UTC](https://discourse.nodered.org/t/time-parsing-how-to-cut-up-time-messages/19485/1 "2019-12-22T23:37:23Z")

</div>

For the sake of this example the payload looks like this:  
`Time 10:23:4`

Shame it isn't better formatted, but anyway.

In light of recent problems I found this problem - or maybe it has only now stuck its head up.

Weirdly it hasn't shown itself until recently.  
This dates back to a problem which happened every day at midnight.  
But that's a whole other story.

So:  
I am sending in the above message to a `function` node.  
I want to determine the "seconds past/since midnight".

This is the code:

```auto
var hour = parseInt(msg.payload.slice(0,2));
var minute = parseInt(msg.payload.slice(3,5));
var second = parseInt(msg.payload.slice(6,8));

node.warn("Time " + msg.payload);
node.warn("Hour " + hour);
node.warn("Minute " + minute);
node.warn("Second " + second);

var time = hour*3600 + minute * 60 + second;
msg = {payload: time, topic: 'RTC'};
return msg;

```

There is a second node which sets the topic as `local` to differentiate between the clock and the RTC.

Here's the _rub_:  
Screen shot 1:  
HWC time parsed by the `function` node. See results.  
Screen shot 2:  
RasPi time parsed by the other `function` node with the same code.  
Sorry I didn't have the `debug` node active.  
However: the errors! Why?  
It is the same code.

 ![Screenshot from 2019-12-23 10-31-54](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c01bfd39c64743647bee897daad11ef7e4e990ef.png)

 ![Screenshot from 2019-12-23 10-35-12](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2337a118eac2753dcf6440c4bee3953bcf61bbdb.png)

Yes, I am missing an elephant.

I accept I maybe should "split" the string with a `:`.  
That aside, why does one work and the other fail?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2019 00:06 UTC](https://discourse.nodered.org/t/time-parsing-how-to-cut-up-time-messages/19485/2 "2019-12-23T00:06:19Z")

</div>

I'm trying something else. Trying to use the _basic_/supplied nodes.

Well, mostly.

So now I have this:  
(example flow)

```auto
[{"id":"ff317fc3.ea97b","type":"inject","z":"55b8031d.59ac1c","name":"Foo","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":1660,"wires":[["dedc1caf.d3e2c8"]]},{"id":"dedc1caf.d3e2c8","type":"function","z":"55b8031d.59ac1c","name":"","func":"msg.payload = new Date().toLocaleString();\nreturn msg;","outputs":1,"noerr":0,"x":200,"y":1660,"wires":[["6e7526e7.96ed1","837e6529.f438f8"]]},{"id":"837e6529.f438f8","type":"moment","z":"55b8031d.59ac1c","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":320,"y":1770,"wires":[["c7a969f7.45794","c7cb6741.8bf48"]]},{"id":"c7cb6741.8bf48","type":"split","z":"55b8031d.59ac1c","name":"","splt":":","spltType":"str","arraySplt":"2","arraySpltType":"len","stream":false,"addname":"blah","x":510,"y":1770,"wires":[["203042c.cf7ce3e"]]},{"id":"203042c.cf7ce3e","type":"debug","z":"55b8031d.59ac1c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":1720,"wires":[]}]

```

Generate the time (yeah, I know I could use the `timestamp` in the `inject` node.

Stick it into a `moment` node to get Hours, minutes and seconds. HH:mm:ss

Send that into a `split` node.  
But reading the docs and what I get are different.

I want an array. (mgs.blah) with the values.  
It isn't happening.

All I am getting is:

```auto
{"topic":"","payload":"11","parts":{"id":"4677f279.e1281c","type":"string","ch":":","index":0,"count":3},"_msgid":"cb34d9db.df4848"}

```

```auto
{"topic":"","payload":"04","parts":{"id":"4677f279.e1281c","type":"string","ch":":","index":1,"count":3},"_msgid":"b09564e3.abe768"}

```

```auto
{"topic":"","payload":"23","parts":{"id":"4677f279.e1281c","type":"string","ch":":","index":2,"count":3},"_msgid":"56a620e4.8798b"}

```

No `msg.blah` visible.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [23 December 2019 00:27 UTC](https://discourse.nodered.org/t/time-parsing-how-to-cut-up-time-messages/19485/3 "2019-12-23T00:27:13Z")

</div>

> [@Trying\_to\_learn](#):
>
> That aside, why does one work and the other fail?

My best guess is that the string being passed to the function may have some leading spaces. It is always a good practice to trim your string. Try using this line as the first one in the function code. The trim method will remove white spaces from both ends of the string.

```auto
msg.payload = msg.payload.trim();

```

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [23 December 2019 00:45 UTC](https://discourse.nodered.org/t/time-parsing-how-to-cut-up-time-messages/19485/4 "2019-12-23T00:45:27Z")

</div>

> [@Trying\_to\_learn](#):
>
> I want an array. (mgs.blah) with the values.  
> It isn't happening.

Indeed. According to the split node help

The behaviour of the node is determined by the type of `msg.payload` :

The output of the moment node is a string, so the split node will disregard the configuration you entered for copying key to msg.blah.

You want to transform a string into an array. This is similar to what has been discussed in this post

> [@Convert csv string to array](https://discourse.nodered.org/t/convert-csv-string-to-array/19088/4):
>
> Right, but can be fixed by using a join node after the split node, with below config. [a-1]

You can apply the same solution, a split node + join node.

Testing flow:

```auto
[{"id":"5a4f00a0.1539d","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"9496950a.d98538","type":"inject","z":"5a4f00a0.1539d","name":"Foo","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":280,"wires":[["a8bc1ecd.231c"]]},{"id":"a8bc1ecd.231c","type":"function","z":"5a4f00a0.1539d","name":"","func":"msg.payload = new Date().toLocaleString();\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":280,"wires":[["e98349ab.6a3558"]]},{"id":"e98349ab.6a3558","type":"moment","z":"5a4f00a0.1539d","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":420,"y":280,"wires":[["6f0835.a09d27cc"]]},{"id":"f0b653ec.00866","type":"debug","z":"5a4f00a0.1539d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":850,"y":280,"wires":[]},{"id":"6f0835.a09d27cc","type":"split","z":"5a4f00a0.1539d","name":"","splt":":","spltType":"str","arraySplt":"2","arraySpltType":"len","stream":false,"addname":"","x":590,"y":280,"wires":[["79820f63.5886a"]]},{"id":"79820f63.5886a","type":"join","z":"5a4f00a0.1539d","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":710,"y":280,"wires":[["f0b653ec.00866"]]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 December 2019 01:01 UTC](https://discourse.nodered.org/t/time-parsing-how-to-cut-up-time-messages/19485/5 "2019-12-23T01:01:30Z")

</div>

@Andrei  
Thanks.

I have since nutted it out.

Though thanks for the help.

This is how I "cheat" to get the time/s working better/how I want:

```auto
[{"id":"837e6529.f438f8","type":"moment","z":"55b8031d.59ac1c","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":380,"y":1660,"wires":[["94444dfb.cd03f"]]},{"id":"dedc1caf.d3e2c8","type":"function","z":"55b8031d.59ac1c","name":"","func":"msg.payload = new Date().toLocaleString();\nreturn msg;","outputs":1,"noerr":0,"x":200,"y":1660,"wires":[["837e6529.f438f8"]]},{"id":"94444dfb.cd03f","type":"function","z":"55b8031d.59ac1c","name":"","func":"var hour;\nvar minute;\nvar second;\nvar spm;\n\nvar times = msg.payload.split(':');\nhour = times[0];\nhour = hour * 3600;\n//node.warn(\"Hours \" + hour);\n\nminute = times[1];\nminute = minute * 60;\n//node.warn(\"Minute \" + minute);\n\nsecond = times[2];\n//node.warn(\"Second \" + second);\n\nspm = parseInt(hour + minute + second);\n\nmsg.payload = spm;\nmsg.topic = \"local\";\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":1660,"wires":[["c320690c.08a068"]]},{"id":"ff317fc3.ea97b","type":"inject","z":"55b8031d.59ac1c","name":"Foo","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":1660,"wires":[["dedc1caf.d3e2c8"]]},{"id":"c320690c.08a068","type":"debug","z":"55b8031d.59ac1c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":700,"y":1660,"wires":[]}]

```

That works.

What was also catching me was that one of the two (software or hardware) time was a _string_ and so I had to add the line `ParseInt( )` to the `function` node to get all things equal.

If I wasn't so stupid I would say there are gremlins that come in and change the code when I am not looking only to spite me.
