# Parse Date/Time IEC 62056-21

**URL:** <https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238>\
**Category:** General\
**Created:** [4 November 2021 23:44 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238 "2021-11-04T23:44:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)\
**Post date:** [4 November 2021 23:44 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/1 "2021-11-04T23:44:10Z")

</div>

Hi,  
I’m trying to extract the date/time from a device that uses IEC 62056 protocol. I can use the buffer/parser node to split the data to output the time, but I can’t work out how I get the date. I’m sure the answers simple, but I just can’t see it.

How do i calculate the date from the string sent by the device?

Examples:  
String from meter = 22080670B00021  
Actual date/time =2021-10-30 06:08:22

08000871D00021  
31 -10-2021 08:00:08

46191541110021  
2021-11-01 15:19:46

50421942310021  
2021-11-02 19:42:50

16510643510021  
2021-11-03 06:51:16

472823 44710021  
2021-11-04 23:28:47

---

<div class="post-metadata">

**Author:** ![RootShell-coder](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rootshell-coder/32/47167_2.png) [@RootShell-coder](https://discourse.nodered.org/u/RootShell-coder)\
**Post date:** [5 November 2021 02:30 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/2 "2021-11-05T02:30:39Z")

</div>

I'm not sure, but it looks like you are getting the wrong data. Perhaps it has something to do with the port's connection speed.

---

<div class="post-metadata">

**Author:** ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)\
**Post date:** [5 November 2021 07:27 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/3 "2021-11-05T07:27:15Z")

</div>

It’s the correct data, the times easy to decide but the date doesn't make sense to me. If you -40 from the first byte you get the date (70 - 40 = 30th) and this pattern works for with all the examples, but how do i get 10/2021 from B00021?

---

<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:** [5 November 2021 08:43 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/4 "2021-11-05T08:43:46Z")

</div>

I think i it is parsed wrong and you should look at settings in buffer parser.

Here is a function i think will produce the date and time.

```auto
[{"id":"c39a375a.75ef88","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"08000871D00021","payloadType":"str","x":130,"y":80,"wires":[["655dab49.00c5dc"]]},{"id":"655dab49.00c5dc","type":"template","z":"b779de97.b1b46","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"22080670B00021\n08000871D00021\n46191541110021\n50421942310021\n16510643510021\n47282344710021","output":"str","x":160,"y":140,"wires":[["7481eb62.24cbac"]]},{"id":"7481eb62.24cbac","type":"split","z":"b779de97.b1b46","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":300,"y":160,"wires":[["c0cbe344.c0da68"]]},{"id":"c0cbe344.c0da68","type":"function","z":"b779de97.b1b46","name":"","func":"let d = msg.payload.split(\"\");\nd[6] = String(d[6]-4);\nd[8] = \"1\";\nd[10] = \"2\";\nd = d.join(\"\").match(/.{2}/g);\nmsg.payload = `${d[3]}-${d[4]}-${d[5] + d[6]} ${d[2]}:${d[1]}:${d[0]}`;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":100,"wires":[["5b7b3dbe.2f2e4c"]]},{"id":"5b7b3dbe.2f2e4c","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":120,"wires":[]}]

```

from the strings you have.

---

<div class="post-metadata">

**Author:** ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)\
**Post date:** [5 November 2021 20:09 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/5 "2021-11-05T20:09:18Z")

</div>

That’s it!!!! Can’t thank you enough for this!!!

---

<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:** [5 November 2021 20:23 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/6 "2021-11-05T20:23:27Z")

</div>

It's not perfect as there may be some tweaking for single digit months. Would need to see jan and feb strings to see if it's possible to handle them.

---

<div class="post-metadata">

**Author:** ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)\
**Post date:** [5 November 2021 20:40 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/7 "2021-11-05T20:40:02Z")

</div>

Ah yes i see, i have an example for January 2021 and its slightly off.

String from meter: 00322041810021  
2021-01-01 20:32:00

This is from the debug:  
01-11-2021 20:32:00

---

<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:** [5 November 2021 20:56 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/8 "2021-11-05T20:56:50Z")

</div>

Think it's possible, can i have a few more examples for each month just to be sure.

[edit] Give me a few mins I think i have an idea

Try this

```auto
[{"id":"c39a375a.75ef88","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"08000871D00021","payloadType":"str","x":200,"y":60,"wires":[["655dab49.00c5dc"]]},{"id":"655dab49.00c5dc","type":"template","z":"b779de97.b1b46","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"22080670B00021\n08000871D00021\n46191541110021\n50421942310021\n16510643510021\n47282344710021\n00322041810021","output":"str","x":230,"y":120,"wires":[["7481eb62.24cbac"]]},{"id":"7481eb62.24cbac","type":"split","z":"b779de97.b1b46","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":370,"y":140,"wires":[["c0cbe344.c0da68"]]},{"id":"c0cbe344.c0da68","type":"function","z":"b779de97.b1b46","name":"","func":"let d = msg.payload.split(\"\");\nd[6] = String(d[6]-4);\nd[8] = (d[8] === \"D\" || d[8] === \"B\" || Number(d[8]) < 8? \"1\" : \"0\");\nd[10] = \"2\";\nd = d.join(\"\").match(/.{2}/g);\nmsg.payload = `${d[3]}-${d[4]}-${d[5] + d[6]} ${d[2]}:${d[1]}:${d[0]}`;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":410,"y":80,"wires":[["5b7b3dbe.2f2e4c"]]},{"id":"5b7b3dbe.2f2e4c","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":100,"wires":[]}]

```

edited to check if char 9 is less than 8.  
I think more data is needed.

---

<div class="post-metadata">

**Author:** ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)\
**Post date:** [5 November 2021 22:02 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/9 "2021-11-05T22:02:23Z")

</div>

24312141020021 = 1st Feb 2021

19312141030021 = 1st March 2021

15312141490021 = 1st Sep 2021

20312190100022 = 10th Oct 2022

---

<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:** [5 November 2021 22:25 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/10 "2021-11-05T22:25:12Z")

</div>

I can cope with months 3, 4, 5, 6, 7, 8 ,9, 10, but months 1, 2, 11, 12 will need to see more data.  
Then there is the issue of 2022 and days being 80 out.

I think this is a no go.

I really think you need to look at the incoming buffer and how you are parsing it.

or

Are these live reading? Could you not use the current date?

---

<div class="post-metadata">

**Author:** ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)\
**Post date:** [6 November 2021 10:43 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/11 "2021-11-06T10:43:35Z")

</div>

It’s live readings from the device, eventually I want to compare the time/date in the device with the actual time (Time stamp) for it flag any discrepancies.

---

<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:** [20 November 2021 10:44 UTC](https://discourse.nodered.org/t/parse-date-time-iec-62056-21/53238/12 "2021-11-20T10:44:04Z")

</div>

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