# Date time into byte array for modbus protocol

**URL:** <https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993>\
**Category:** Dashboard\
**Created:** [18 November 2018 05:04 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993 "2018-11-18T05:04:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rox3657](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rox3657/32/3847_2.png) [@rox3657](https://discourse.nodered.org/u/rox3657)\
**Post date:** [18 November 2018 05:04 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/1 "2018-11-18T05:04:04Z")

</div>

Hi, can somebody help me, i’m using node-red-contrib modbus and I have a button on dashboard that when actuated will synchronize the datetime between node red and my controller, need help to create Function to pack the Date & Time into byte array to send via modbus protocol from RPI to my controller, it would be 2 bytes per Modbus register.

Rox

---

<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 November 2018 07:09 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/2 "2018-11-18T07:09:46Z")

</div>

To answer that we need to know exactly what format the device expects for the time.

---

<div class="post-metadata">

**Author:** ![rox3657](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rox3657/32/3847_2.png) [@rox3657](https://discourse.nodered.org/u/rox3657)\
**Post date:** [18 November 2018 08:20 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/3 "2018-11-18T08:20:44Z")

</div>

I just want to put last 2 digit of a year in byte0 and the month value in byte1, day value in byte2, hour value in byte3,mins. value in byte4, secs value in byte5; byte 0 and 1 will be one 16 Bit unsigned integer, byte1 & 2, next 16 bit integer.. i will then input it to a modbus node.

Thanks for looking.

---

<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 November 2018 10:02 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/4 "2018-11-18T10:02:11Z")

</div>

Which bit don't you know how to do? Getting the year, month etc or packing them into the integers?  
If you don't know how to the get the date/time values then have a look at [1]. Have a play with those methods in a function node and see how you get on. While experimenting you can use node.warn to show values in the debug panel. So you might start with

```auto
let now = new Date();
let year = now.getUTCFullYear();
node.warn("year is " + year);

```

[1] [https://www.w3schools.com/jsref/jsref\_obj\_date.asp](https://www.w3schools.com/jsref/jsref_obj_date.asp)

---

<div class="post-metadata">

**Author:** ![rox3657](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rox3657/32/3847_2.png) [@rox3657](https://discourse.nodered.org/u/rox3657)\
**Post date:** [18 November 2018 13:23 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/5 "2018-11-18T13:23:41Z")

</div>

Thank you Colin for giving me direction.. now i can write the year to my controller , now i need to pack it to integer with the one byte containing the month, can you please show me. here's what I did.

let now = new Date();  
let year = now.getUTCFullYear();  
let month = now.setUTCMonth();  
msg.payload = year - 2000;  
node.warn( msg);  
return msg;

---

<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 November 2018 13:30 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/6 "2018-11-18T13:30:14Z")

</div>

Something like

```auto
let monthYear = year - 2000 + (month << 8);

```

That shifts the month value left 8 bits before adding it to the year which should be what you want I think. You will be in trouble after the year 2127 but I don't suppose you will lose much sleep over that. Of course that is exactly what the first programmers thought about the year 2000 🙂

---

<div class="post-metadata">

**Author:** ![rox3657](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rox3657/32/3847_2.png) [@rox3657](https://discourse.nodered.org/u/rox3657)\
**Post date:** [18 November 2018 13:30 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/7 "2018-11-18T13:30:40Z")

</div>

I realized i used set instead of get just after posting it. im still trying.

---

<div class="post-metadata">

**Author:** ![rox3657](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rox3657/32/3847_2.png) [@rox3657](https://discourse.nodered.org/u/rox3657)\
**Post date:** [18 November 2018 13:33 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/8 "2018-11-18T13:33:15Z")

</div>

Thank you ill try that, its just a hobby project so just getting started, so after its running ill keep improving it, Ill remember ur advise about 2127.

---

<div class="post-metadata">

**Author:** ![rox3657](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rox3657/32/3847_2.png) [@rox3657](https://discourse.nodered.org/u/rox3657)\
**Post date:** [18 November 2018 13:50 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/9 "2018-11-18T13:50:55Z")

</div>

Im getting there! only the strange thing ,is i'm getting 10 instead of 11 for the month.

---

<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 November 2018 13:53 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/10 "2018-11-18T13:53:27Z")

</div>

> [@rox3657](#):
>
> i'm getting 10 instead of 11 for the month

Look again at the function definition in the link I posted.

---

<div class="post-metadata">

**Author:** ![rox3657](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rox3657/32/3847_2.png) [@rox3657](https://discourse.nodered.org/u/rox3657)\
**Post date:** [18 November 2018 13:56 UTC](https://discourse.nodered.org/t/date-time-into-byte-array-for-modbus-protocol/4993/11 "2018-11-18T13:56:25Z")

</div>

I got it! I can now take it from here.  
thank you very much!
