# How to join numbers to form a date?

**URL:** <https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655>\
**Category:** General\
**Created:** [1 December 2019 13:45 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655 "2019-12-01T13:45:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://discourse.nodered.org/u/Phil)\
**Post date:** [1 December 2019 13:45 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/1 "2019-12-01T13:45:01Z")

</div>

I have 3 numbers say 1,12, 2019 coming from different nodes at the same time. I need to join them to form a date like 1/12/2019. how to do it?  
I tried to save the numbers in context.data. and then tried to join them in function but the following error coming. "Function tried to send a message of type number".  
can anyone help????

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 December 2019 13:55 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/2 "2019-12-01T13:55:45Z")

</div>

Hi @Phil

if you can share the code you've tried so far it'll be easier to see where you may have gone wrong.

The error you are getting is because you are returning a Number from the function, rather than a message object.

Typically you would set `msg.payload` to the result you want and then `return msg`.

```auto
msg.payload = numberA+numberB;
return msg;

```

Now, given your description, it sounds like you are accidentally adding the numbers rather than joining them together to form a string of text.

So lets say you have three variables `day`, `month`, `year` in the function, to create the string you want, you would do:

```auto
msg.payload = day + "/" + month + "/" + year;
return msg;

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 December 2019 13:56 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/3 "2019-12-01T13:56:46Z")

</div>

> [@Phil](#):
>
> "Function tried to send a message of type number".

you can onlly send an object. You probablu had `msg = your-variable` Try using `msg.payload = your-variable;` instead

EDIT: beaten by a few seconds...

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://discourse.nodered.org/u/Phil)\
**Post date:** [1 December 2019 14:25 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/4 "2019-12-01T14:25:26Z")

</div>

I tried join node now. it almost worked.  
Each time the sequence changes.  
ist it shows date and time next it shows time and date

 ![date](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c5fc4c54ccdf43802a8ce574b77515aa8dfcb986.png) .

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://discourse.nodered.org/u/Phil)\
**Post date:** [1 December 2019 14:42 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/5 "2019-12-01T14:42:57Z")

</div>

Tried msg.payload. Partial success.. each time it detect only one variable.

 ![function](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1da543fd5c0b696470e71872c38d55643675fb4a.png)

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://discourse.nodered.org/u/Phil)\
**Post date:** [1 December 2019 14:43 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/6 "2019-12-01T14:43:42Z")

</div>

[{"id":"8db6b72e.27dbd8","type":"inject","z":"1885d08f.0f234f","name":"","topic":"date","payload":"1","payloadType":"num","repeat":"3","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":260,"wires":[["52b0e3c7.75877c"]]},{"id":"41ea716c.3d6e7","type":"inject","z":"1885d08f.0f234f","name":"","topic":"month","payload":"12","payloadType":"num","repeat":"3","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":320,"wires":[["52b0e3c7.75877c"]]},{"id":"67719556.f2f8bc","type":"inject","z":"1885d08f.0f234f","name":"","topic":"year","payload":"2019","payloadType":"num","repeat":"3","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":380,"wires":[["52b0e3c7.75877c"]]},{"id":"4b7e5e37.2f66e","type":"debug","z":"1885d08f.0f234f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":510,"y":180,"wires":},{"id":"52b0e3c7.75877c","type":"function","z":"1885d08f.0f234f","name":"","func":"context.data=context.data||{};\nswitch (msg.topic){\n case"date":\n context.data.x1=msg.payload;\n var x=context.data.x1\n break;\n case"month":\n context.data.x2=msg.payload\n var y=context.data.x2;\n break;\n case"year":\n context.data.x3=msg.payload\n var z=context.data.x3;\n break;\n}\nmsg.payload=(x+'/'+y+'/'+z)\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":280,"wires":[["4b7e5e37.2f66e"]]}]

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://discourse.nodered.org/u/Phil)\
**Post date:** [1 December 2019 14:46 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/7 "2019-12-01T14:46:34Z")

</div>

Tried 2 methods so far using functions and using join node. both partially success. code is given below

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 December 2019 16:23 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/8 "2019-12-01T16:23:27Z")

</div>

please read [this thread](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) and edit your previous post. Currently your flow is not importable.

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://discourse.nodered.org/u/Phil)\
**Post date:** [1 December 2019 21:03 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/9 "2019-12-01T21:03:40Z")

</div>

This is using the join node. The output is ok but at first its date and time and in the next cycle its time and date and keeps changing

```auto
[
    {
        "id": "d694ebde.152018",
        "type": "join",
        "z": "22029cc5.20e2c4",
        "name": "",
        "mode": "custom",
        "build": "string",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "/",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "3",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 350,
        "y": 200,
        "wires": [
            [
                "9518bd97.5414e"
            ]
        ]
    },
    {
        "id": "c0a1e9.ac986e18",
        "type": "inject",
        "z": "22029cc5.20e2c4",
        "name": "",
        "topic": "date",
        "payload": "1",
        "payloadType": "num",
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 120,
        "y": 180,
        "wires": [
            [
                "d694ebde.152018"
            ]
        ]
    },
    {
        "id": "adfe84f7.a74d48",
        "type": "inject",
        "z": "22029cc5.20e2c4",
        "name": "",
        "topic": "month",
        "payload": "12",
        "payloadType": "num",
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 130,
        "y": 240,
        "wires": [
            [
                "d694ebde.152018"
            ]
        ]
    },
    {
        "id": "f39280bd.6653a",
        "type": "inject",
        "z": "22029cc5.20e2c4",
        "name": "",
        "topic": "year",
        "payload": "2019",
        "payloadType": "num",
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 130,
        "y": 300,
        "wires": [
            [
                "d694ebde.152018"
            ]
        ]
    },
    {
        "id": "9518bd97.5414e",
        "type": "join",
        "z": "22029cc5.20e2c4",
        "name": "",
        "mode": "custom",
        "build": "string",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "-",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "3",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 480,
        "y": 260,
        "wires": [
            [
                "b2d96c47.8615f"
            ]
        ]
    },
    {
        "id": "45deec6.ab63214",
        "type": "join",
        "z": "22029cc5.20e2c4",
        "name": "",
        "mode": "custom",
        "build": "string",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": ":",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "3",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 300,
        "y": 360,
        "wires": [
            [
                "9518bd97.5414e"
            ]
        ]
    },
    {
        "id": "b2d96c47.8615f",
        "type": "debug",
        "z": "22029cc5.20e2c4",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 530,
        "y": 80,
        "wires": []
    },
    {
        "id": "91bc984e.dec6a8",
        "type": "inject",
        "z": "22029cc5.20e2c4",
        "name": "",
        "topic": "HOUR",
        "payload": "7",
        "payloadType": "num",
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 120,
        "y": 360,
        "wires": [
            [
                "45deec6.ab63214"
            ]
        ]
    },
    {
        "id": "98346777.0ad168",
        "type": "inject",
        "z": "22029cc5.20e2c4",
        "name": "",
        "topic": "MIN",
        "payload": "45",
        "payloadType": "num",
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 120,
        "y": 400,
        "wires": [
            [
                "45deec6.ab63214"
            ]
        ]
    },
    {
        "id": "6278c2d8.7df94c",
        "type": "inject",
        "z": "22029cc5.20e2c4",
        "name": "",
        "topic": "SEC",
        "payload": "10",
        "payloadType": "num",
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 120,
        "y": 440,
        "wires": [
            [
                "45deec6.ab63214"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://discourse.nodered.org/u/Phil)\
**Post date:** [1 December 2019 21:07 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/10 "2019-12-01T21:07:32Z")

</div>

This one is using the function node. It's partially working.

```auto
[
    {
        "id": "8db6b72e.27dbd8",
        "type": "inject",
        "z": "1885d08f.0f234f",
        "name": "",
        "topic": "date",
        "payload": "1",
        "payloadType": "num",
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 160,
        "y": 260,
        "wires": [
            [
                "52b0e3c7.75877c"
            ]
        ]
    },
    {
        "id": "41ea716c.3d6e7",
        "type": "inject",
        "z": "1885d08f.0f234f",
        "name": "",
        "topic": "month",
        "payload": "12",
        "payloadType": "num",
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 170,
        "y": 320,
        "wires": [
            [
                "52b0e3c7.75877c"
            ]
        ]
    },
    {
        "id": "67719556.f2f8bc",
        "type": "inject",
        "z": "1885d08f.0f234f",
        "name": "",
        "topic": "year",
        "payload": "2019",
        "payloadType": "num",
        "repeat": "3",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 170,
        "y": 380,
        "wires": [
            [
                "52b0e3c7.75877c"
            ]
        ]
    },
    {
        "id": "4b7e5e37.2f66e",
        "type": "debug",
        "z": "1885d08f.0f234f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 510,
        "y": 180,
        "wires": []
    },
    {
        "id": "52b0e3c7.75877c",
        "type": "function",
        "z": "1885d08f.0f234f",
        "name": "",
        "func": "context.data=context.data||{};\nswitch (msg.topic){\n case\"date\":\n context.data.x1=msg.payload;\n var x=context.data.x1\n break;\n case\"month\":\n context.data.x2=msg.payload\n var y=context.data.x2;\n break;\n case\"year\":\n context.data.x3=msg.payload\n var z=context.data.x3;\n break;\n}\nmsg.payload=(x+'/'+y+'/'+z)\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 350,
        "y": 280,
        "wires": [
            [
                "4b7e5e37.2f66e"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 December 2019 21:49 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/11 "2019-12-01T21:49:27Z")

</div>

> [@Phil](#):
>
> This is using the join node. The output is ok but at first its date and time and in the next cycle its time and date and keeps changing

How many items are you joining? check all three join nodes

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://discourse.nodered.org/u/Phil)\
**Post date:** [1 December 2019 22:17 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/12 "2019-12-01T22:17:24Z")

</div>

1st 3 nodes joined to give date as DD/MM/YYYY.  
next 3 nodes joined to give time as HH:MM: SS.  
Then both date and time are joined in the next join node. actually they are getting joined but the problem is one time its as DD/MM/YYYY- HH:MM: SS next time it gets reversed as HH:MM:SS-DD/MM/YYYY. you can see it in debug output.

 ![date](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c5fc4c54ccdf43802a8ce574b77515aa8dfcb986.png)

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://discourse.nodered.org/u/Phil)\
**Post date:** [1 December 2019 22:31 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/13 "2019-12-01T22:31:44Z")

</div>

I found out the error.  
In the last join node _"After a number of message parts"_ column was filled with _"3"_ instead of "2" after copy-pasting from the older one. Sorry, my bad.  
Anyway, I really appreciate the quick replies. Thank you for your help.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 December 2019 22:37 UTC](https://discourse.nodered.org/t/how-to-join-numbers-to-form-a-date/18655/14 "2019-12-01T22:37:32Z")

</div>

No problem, I saw the problem in the third join but wanted you to discover it yourself. 👍
