# 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:** 20\
**Page:** 1

<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 08:53 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/1 "2023-03-19T08:53:16Z")

</div>

[node-red-utc-help-demo.json](https://discourse.nodered.org/uploads/short-url/eCqS5mp23z2vcyKaLtWTxnVWTdM.json) (3.8 KB)

Convert using function  
utc hex to time in this format  
2023-3-19 14:20:14

I am able to convert the code  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c39b706c1d63a01a40f85316b55de47ed3259e7.png)

after that in the defined format I am unable to do

```auto
[
    {
        "id": "f8a89ada4bd4d49b",
        "type": "inject",
        "z": "478d88efdb005c1e",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"msg\":\"dAck\",\"mac\":\"BC572900A05B\",\"rslt\":\"succ\",\"seq\":7716,\"cause\":0,\"rmn\":1,\"dType\":\"hex\",\"data\":\"0200F502000000F0632422491E8A31686324226D1F0D3139632422B020952D7C632422E020C22A6E63242452204B28F46324277020B7292363242EBE2045287563243D341FCC2907632442C61F4A28BD63244DE11ECC287A63245A1C1E4C2851632460591DCB2830632483931D4C26D663248A401CC92626632490351C4925A2632497291BCA25A96324A2981B4A25B96324B2311ACB259A6324D86C1A4B25BE6324E1D319CB25806324EFC8194C25826324F82219B327736324FD9C1A3627A0632501CE1ABA27D26325097A1B33277463250D611BB727CD632510561C412822632512D21CB42858632515C11D34288F63251A961CCB271A\",\"gmac\":\"94A408B0482C\"}",
        "payloadType": "json",
        "x": 170,
        "y": 100,
        "wires": [
            [
                "d6488bee8efe8fdc",
                "55a47454709a309f"
            ]
        ]
    },
    {
        "id": "d6488bee8efe8fdc",
        "type": "function",
        "z": "478d88efdb005c1e",
        "name": "function 24",
        "func": " \n var newMsg = {};\n \n \n \n\nvar data_time = msg.payload.data.substr(16, 8) ;\nvar Temp = Number(hexTo8p8(msg.payload.data.substr(16+8, 4)));\nvar Humidity = Number(hexTo8p8(msg.payload.data.substr(16 + 8+4, 4)));\nvar data = msg.payload.data.substr(16, 8);\n\n for(var i=1;i<=30;i++)\n {\n data_time = msg.payload.data.substr(16 * i, 8 );\n Temp = Number(hexTo8p8(msg.payload.data.substr(24 * i, 4 )));\n Humidity = Number(hexTo8p8(msg.payload.data.substr(28 * i, 4 )));\n data = msg.payload.data.substr(16 * i, 8 );\n \n var checktime = 1546337502;\n\n if(checktime>data_time)\n {\n\n }\n\n \n newMsg.payload = [{\n \n time: data_time,\n utc: Buffer.from(data_time, 'hex').toString(),\n \n temp: Temp,\n humidity: Humidity,\n \n },\n {\n\n history: 1 \n \n\n\n\n }];\n node.send(newMsg);\n\n \n } \n\n\n \n \n \n \n \n \n\n\nreturn null;\n\nfunction calculateDistance(rssi) {\n let P = -69; // @TODO This value should come from MQTT message\n let n = 3;\n let d = Math.pow(10, ((P - rssi) / (10 * n))); //(n ranges from 2 to 4)\n return d * 2;\n}\n\n\nfunction hexTo8p8(/**@type {string} */ value) {\n let intValue = parseInt(value, 16);\n let signed = (intValue & 0x8000) > 0 ? -1/10 : 1;\n return signed * intValue / Math.pow(2, 8);\n}\n\n\n\nfunction int2float( /** @type {string} */value_int) {\n // Create new Buffer based on array bytes\n const buf = Buffer.from(value_int);\n // swap the words\n const temp0 = buf[0];\n const temp1 = buf[1];\n buf[0] = buf[2];\n buf[1] = buf[3];\n buf[2] = temp0;\n buf[3] = temp1;\n\n // Represent these bytes as 32-bit unsigned int\n const value = buf.readInt32BE();\n\n // save the value\n msg.payload = value;\n\n return msg;\n}\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 410,
        "y": 160,
        "wires": [
            [
                "caf2f9fe83302084"
            ]
        ]
    },
    {
        "id": "caf2f9fe83302084",
        "type": "debug",
        "z": "478d88efdb005c1e",
        "name": "debug 336",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 610,
        "y": 180,
        "wires": []
    }
]

```

---

<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:** [19 March 2023 08:54 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/2 "2023-03-19T08:54:20Z")

</div>

Only to _put it out there_:  
There is a `moment` node that does this for you.

---

<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 08:55 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/3 "2023-03-19T08:55:48Z")

</div>

how can i use the moment node in my function ,

newMsg.payload = [{

```
      time: data_time,
      utc: Buffer.from(data_time, 'hex').toString() **<--- Moment code eg ??**

      temp: Temp,
      humidity: Humidity,
   
  },

```

---

<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:** [19 March 2023 08:58 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/4 "2023-03-19T08:58:47Z")

</div>

Well, rather than IN your function node:  
Just before it when the message is _arriving_ and the time is.... in .... the message.  
Stick the `moment` node in and get it to return human readable time.

Then send the message to your node and don't bother doing the time stuff.  
The _time_ would be already human readable.

Just a thought.

# OK, sorry.

the time isn't in the message.

Stick a `timestamp` into the message (`change` node. Set a message part to `timestamp`)  
Send it through the `moment` node then into your `function` node.  
(I can't/don't always read things and sometimes the parsing isn't the best.)

---

<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:02 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/5 "2023-03-19T09:02:19Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/85a98e1f36c751e5b5070bcb8a75ac5a95c6bfd9.png)  
I am getting in UTC hex

---

<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:03 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/6 "2023-03-19T09:03:49Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/523e4ebab65cd608d64226ccc2c506cba81e89b0.png)

and I have to parse the data from hex then convert in 8.8 floating point format then insert into time series

better be able to do inside my function

---

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

</div>

Ok, sorry.

I kinda jumped in there/here feet first.

Seems a weird _standard_ for time. But ....

I hope someone else can help you.

---

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

</div>

Do you know how to PASTE the exact/actual message here?  
(formatting, etc)

---

<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:07 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/9 "2023-03-19T09:07:44Z")

</div>

[https://discourse.nodered.org/uploads/short-url/eCqS5mp23z2vcyKaLtWTxnVWTdM.json](https://discourse.nodered.org/uploads/short-url/eCqS5mp23z2vcyKaLtWTxnVWTdM.json)

import this

---

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

</div>

Oh sorry. You posted it at the top as an attachment.

You can paste code here.

Look at/in the top of the EDIT window.

The `</>` button.

(Or press the _back tick_ button 3 times) The one just to the left of the `1` key.  
Paste the code.  
Press enter.  
Repeat the 3 _back ticks_  
press enter.

It should look something like:

```auto
This is where you code is.

```

Oh when you EXPORT the code, click on the JSON button and down the bottom select COMPACT.

---

<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:17 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/11 "2023-03-19T09:17:14Z")

</div>

```auto
[{"id":"f8a89ada4bd4d49b","type":"inject","z":"478d88efdb005c1e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"msg\":\"dAck\",\"mac\":\"BC572900A05B\",\"rslt\":\"succ\",\"seq\":7716,\"cause\":0,\"rmn\":1,\"dType\":\"hex\",\"data\":\"0200F502000000F0632422491E8A31686324226D1F0D3139632422B020952D7C632422E020C22A6E63242452204B28F46324277020B7292363242EBE2045287563243D341FCC2907632442C61F4A28BD63244DE11ECC287A63245A1C1E4C2851632460591DCB2830632483931D4C26D663248A401CC92626632490351C4925A2632497291BCA25A96324A2981B4A25B96324B2311ACB259A6324D86C1A4B25BE6324E1D319CB25806324EFC8194C25826324F82219B327736324FD9C1A3627A0632501CE1ABA27D26325097A1B33277463250D611BB727CD632510561C412822632512D21CB42858632515C11D34288F63251A961CCB271A\",\"gmac\":\"94A408B0482C\"}","payloadType":"json","x":170,"y":100,"wires":[["d6488bee8efe8fdc","55a47454709a309f"]]},{"id":"d6488bee8efe8fdc","type":"function","z":"478d88efdb005c1e","name":"function 24","func":" \n var newMsg = {};\n \n \n \n\nvar data_time = msg.payload.data.substr(16, 8) ;\nvar Temp = Number(hexTo8p8(msg.payload.data.substr(16+8, 4)));\nvar Humidity = Number(hexTo8p8(msg.payload.data.substr(16 + 8+4, 4)));\nvar data = msg.payload.data.substr(16, 8);\n\n for(var i=1;i<=30;i++)\n {\n data_time = msg.payload.data.substr(16 * i, 8 );\n Temp = Number(hexTo8p8(msg.payload.data.substr(24 * i, 4 )));\n Humidity = Number(hexTo8p8(msg.payload.data.substr(28 * i, 4 )));\n data = msg.payload.data.substr(16 * i, 8 );\n \n var checktime = 1546337502;\n if(checktime>data_time)\n {\n\n }\n\n \n newMsg.payload = [{\n \n time: data_time,\n utc: \"\",\n \n temp: Temp,\n humidity: Humidity,\n \n },\n {\n\n history: 1 \n \n\n\n\n }];\n node.send(newMsg);\n\n \n } \n\n\n \n \n \n \n \n \n\n\nreturn null;\n\nfunction calculateDistance(rssi) {\n let P = -69; // @TODO This value should come from MQTT message\n let n = 3;\n let d = Math.pow(10, ((P - rssi) / (10 * n))); //(n ranges from 2 to 4)\n return d * 2;\n}\n\n\nfunction hexTo8p8(/**@type {string} */ value) {\n let intValue = parseInt(value, 16);\n let signed = (intValue & 0x8000) > 0 ? -1/10 : 1;\n return signed * intValue / Math.pow(2, 8);\n}\n\n\n\nfunction int2float( /** @type {string} */value_int) {\n // Create new Buffer based on array bytes\n const buf = Buffer.from(value_int);\n // swap the words\n const temp0 = buf[0];\n const temp1 = buf[1];\n buf[0] = buf[2];\n buf[1] = buf[3];\n buf[2] = temp0;\n buf[3] = temp1;\n\n // Represent these bytes as 32-bit unsigned int\n const value = buf.readInt32BE();\n\n // save the value\n msg.payload = value;\n\n return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":160,"wires":[["caf2f9fe83302084"]]},{"id":"caf2f9fe83302084","type":"debug","z":"478d88efdb005c1e","name":"debug 336","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":180,"wires":[]}]

```

---

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

</div>

Ok,  
It does work.

Could you please install the `moment` node?  
(Or: Do you have the `moment` mode installed already?  
`node-red-contrib-moment`

And do you know how to import flows from other people?

---

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

</div>

yes installed

---

<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:19 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/14 "2023-03-19T09:19:39Z")

</div>

yes i can import

---

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

</div>

Sorry. I just didn't want to presume.

Try this.

```auto
[{"id":"f8a89ada4bd4d49b","type":"inject","z":"82a373101348c1cf","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"msg\":\"dAck\",\"mac\":\"BC572900A05B\",\"rslt\":\"succ\",\"seq\":7716,\"cause\":0,\"rmn\":1,\"dType\":\"hex\",\"data\":\"0200F502000000F0632422491E8A31686324226D1F0D3139632422B020952D7C632422E020C22A6E63242452204B28F46324277020B7292363242EBE2045287563243D341FCC2907632442C61F4A28BD63244DE11ECC287A63245A1C1E4C2851632460591DCB2830632483931D4C26D663248A401CC92626632490351C4925A2632497291BCA25A96324A2981B4A25B96324B2311ACB259A6324D86C1A4B25BE6324E1D319CB25806324EFC8194C25826324F82219B327736324FD9C1A3627A0632501CE1ABA27D26325097A1B33277463250D611BB727CD632510561C412822632512D21CB42858632515C11D34288F63251A961CCB271A\",\"gmac\":\"94A408B0482C\"}","payloadType":"json","x":370,"y":1000,"wires":[["d6488bee8efe8fdc","d1c33f17d410f135"]]},{"id":"d1c33f17d410f135","type":"moment","z":"82a373101348c1cf","name":"","topic":"","input":"data","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"en-AU","output":"data","outputType":"msg","outTz":"Australia/Sydney","x":560,"y":1050,"wires":[["cb46a9925f7a74ac"]]},{"id":"cb46a9925f7a74ac","type":"debug","z":"82a373101348c1cf","name":"debug 337","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":1050,"wires":[]}]

```

This is JUST TO GET YOU STARTED.

You will have to put your `function` node after this and do what I said.  
But I am not sure that is the correct date being shown.

---

<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:22 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/16 "2023-03-19T09:22:10Z")

</div>

the function contains instructions to chop data and convert from hex  
in the next step time conversion needs to take place .

---

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

</div>

Yes, but now the time (is that the correct time?) is now in `msg.data` and so you can write the code in the `function` node a lot easier than trying to decode the original _value_.

I guess.

---

<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:28 UTC](https://discourse.nodered.org/t/utc-conversion-using-function/76706/18 "2023-03-19T09:28:39Z")

</div>

The parsing needs to be inside , you have attached a timestamp in the outer block on json.  
this is not what i was asking

---

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

</div>

Try

```auto
let UTC_ISO = new Date(parseInt(msg.payload.data.slice(16, 24), 16) * 1000).toISOString();    
msg.payload = UTC_ISO.split(/\.|T/).slice(0,2).join(" ");
return msg;

```

output  
`2022-09-16 07:14:17`

[edit] removed substr() as deprecated, used slice() instead.

---

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

</div>

Then it is beyond my skills.  
Though I also fear you are not seeing the advantage of what I am saying.

I got the right time - I think. But I am NOT in your timezone. So I am guessing that it did convert the original HUGE number to human readable.

Good luck.

WAY above my skills.

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