# Convert buffer to time and compare with current time

**URL:** https://discourse.nodered.org/t/convert-buffer-to-time-and-compare-with-current-time/72392
**Category:** General
**Created:** [18 December 2022 21:32 UTC](https://discourse.nodered.org/t/convert-buffer-to-time-and-compare-with-current-time/72392 "2022-12-18T21:32:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![walberjunior](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/walberjunior/32/68528_2.png) [@walberjunior](https://discourse.nodered.org/u/walberjunior)
#### Post date: [18 December 2022 21:32 UTC](https://discourse.nodered.org/t/convert-buffer-to-time-and-compare-with-current-time/72392/1 "2022-12-18T21:32:12Z")

</div>

Hi.

Among other data, I get the date and time from a buffer and I need to compare it with the current date.

But even simulating the current date, when I do the conversion there is a very large difference between the date received via buffer and the current date, approximately 125 days

What am I doing wrong?  
How can I do this using function?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b72eddf98e699ec6da73a1bc8757e5d8f23a6aa9.png)

```auto
[{"id":"13ed1b0a6338ba91","type":"inject","z":"b5bfdf7d38fd994f","name":"time date","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"0x12\",\"0x16\",\"0x12\",\"0x0c\",\"0x16\"]","payloadType":"bin","x":700,"y":1020,"wires":[["ceae1eaa6db92208"]]},{"id":"ceae1eaa6db92208","type":"function","z":"b5bfdf7d38fd994f","name":"function 4","func":"var yy;\nvar mm;\nvar dd;\nvar hh;\nvar min;\n\n//year\nif (msg.payload[4] < 10) {\n yy = \"0\" + msg.payload[4]// + \":00\"\n} else {\n\n yy = msg.payload[4]// + \":00\"\n}\n//month\nif (msg.payload[3] < 10) {\n mm = \"0\" + msg.payload[3]// + \":00\"\n} else {\n\n mm = msg.payload[3]// + \":00\"\n}\n//day\nif (msg.payload[2] < 10) {\n dd = \"0\" + msg.payload[2]// + \":00\"\n} else {\n\n dd = msg.payload[2]// + \":00\"\n}\n//hour\nif (msg.payload[0] < 10) {\n hh = \"0\" + msg.payload[0]// + \":00\"\n} else {\n\n hh = msg.payload[0]// + \":00\"\n}\n//minutes\nif (msg.payload[1] < 10) {\n min = \"0\" + msg.payload[1]// + \":00\"\n} else {\n\n min = msg.payload[1]// + \":00\"\n}\n//minutes\nif (msg.payload[1] < 10) {\n min = \"0\" + msg.payload[1]// + \":00\"\n} else {\n\n min = msg.payload[1]// + \":00\"\n}\n\n\nmsg.clock2 = (\n \"20\" + yy + \"-\" +\n mm + \"-\" +\n dd + \"T\" +\n hh + \":\" +\n min + \":00+0000\"\n)\n\nmsg.dif = (((Date.now() - 10798840) - (Date.parse(msg.clock2) - 0)) / 1000);\nmsg.buffer = Date.parse(msg.clock2);\nmsg.now = Date.now();\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":1020,"wires":[["d7f23d72e1130986"]]},{"id":"d7f23d72e1130986","type":"debug","z":"b5bfdf7d38fd994f","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":1020,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [18 December 2022 21:50 UTC](https://discourse.nodered.org/t/convert-buffer-to-time-and-compare-with-current-time/72392/2 "2022-12-18T21:50:51Z")

</div>

If you mean the number underlined, that is 3 hours. It is in milliseconds which is standard for javascript dates. I suspect your time zone is 3 hours from UTC.

---

<div class="post-metadata">

### Author: ![walberjunior](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/walberjunior/32/68528_2.png) [@walberjunior](https://discourse.nodered.org/u/walberjunior)
#### Post date: [18 December 2022 23:21 UTC](https://discourse.nodered.org/t/convert-buffer-to-time-and-compare-with-current-time/72392/3 "2022-12-18T23:21:02Z")

</div>

I suspected it was something stupid.

---

<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: [16 February 2023 23:21 UTC](https://discourse.nodered.org/t/convert-buffer-to-time-and-compare-with-current-time/72392/4 "2023-02-16T23:21:59Z")

</div>

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