# UTC Conversion using Function

**URL:** <https://discourse.nodered.org/t/utc-conversion-using-function/76706>\
**Category:** General\
**Created:** [19 March 2023 08:53 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706 "2023-03-19T08:53:16Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [19 March 2023 09:40 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/21 "2023-03-19T09:40:57Z")

</div>

This works !!!

could you help me understand if we can use the shift operator to read the data from start to end .  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f8d783fc0ca6acbc839b7c2d5ed17f6e0b97e2b.png)  
instead of the loops that has been used

```auto
 
    var newMsg = {};
  
 
 

var data_time = msg.payload.data.substr(16, 8) ;
var Temp = Number(hexTo8p8(msg.payload.data.substr(16+8, 4)));
var Humidity = Number(hexTo8p8(msg.payload.data.substr(16 + 8+4, 4)));
var data = msg.payload.data.substr(16, 8);

  for(var i=1;i<=30;i++)
  {
      data_time = msg.payload.data.substr(16 * i, 8 );
      Temp = Number(hexTo8p8(msg.payload.data.substr(24 * i, 4 )));
      Humidity = Number(hexTo8p8(msg.payload.data.substr(28 * i, 4 )));
      data = msg.payload.data.substr(16 * i, 8 );
      let UTC_ISO = new Date(parseInt(msg.payload.data.substr(16 * i, 8), 16) * 1000).toISOString();
      var converted_payload = UTC_ISO.split(/\.|T/).slice(0, 2).join(" ");
      
      var checktime = 1546337502;
      if(checktime>data_time)
      {

      }

      
      newMsg.payload = [{
        
          time: data_time,
          utc: converted_payload,
 
          temp: Temp,
          humidity: Humidity,
       
      },
      {

          history: 1 
   

      }];
      node.send(newMsg);

 
  }  

 
 
     
        
   
 

return null;

function calculateDistance(rssi) {
    let P = -69; // @TODO This value should come from MQTT message
    let n = 3;
    let d = Math.pow(10, ((P - rssi) / (10 * n))); //(n ranges from 2 to 4)
    return d * 2;
}

function hexTo8p8(/** @type {string} */ value) {
    let intValue = parseInt(value, 16);
    let signed = (intValue & 0x8000) > 0 ? -1/10 : 1;
    return signed * intValue / Math.pow(2, 8);
}

function int2float( /** @type {string} */value_int) {
    // Create new Buffer based on array bytes
    const buf = Buffer.from(value_int);
    // swap the words
    const temp0 = buf[0];
    const temp1 = buf[1];
    buf[0] = buf[2];
    buf[1] = buf[3];
    buf[2] = temp0;
    buf[3] = temp1;

    // Represent these bytes as 32-bit unsigned int
    const value = buf.readInt32BE();

    // save the value
    msg.payload = value;

    return msg;
}

```

---

<div class="post-metadata">

**Author:** ![ankit.prasad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@ankit.prasad](https://discourse.nodered.org/u/ankit.prasad)\
**Post date:** [19 March 2023 09:48 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/22 "2023-03-19T09:48:31Z")

</div>

```auto
Similarly can we convert it to unix time stamp in the same way ?

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 March 2023 09:54 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/23 "2023-03-19T09:54:45Z")

</div>

> [@ankit.prasad](#):
>
> read the data

This looks like a job for node-red-contrib-buffer-parser

---

<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:** [19 March 2023 09:55 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/24 "2023-03-19T09:55:29Z")

</div>

> [@ankit.prasad](#):
>
> I have to parse the data from hex then convert in 8.8 floating point format then insert into time

I think you should probably be using node-red-contrib-buffer-parser to decode your buffer. That node is designed for exactly this sort of issue.

---

<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 May 2023 09:56 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/25 "2023-05-18T09:56:15Z")

</div>

Closed on 18 May 2023 09:56 UTC

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

[Previous page](https://discourse.nodered.org/t/utc-conversion-using-function/76706.md?page=1)
