How to join numbers to form a date?

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????

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.

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:

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

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...

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

.

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

[{"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"]]}]

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

please read this thread and edit your previous post. Currently your flow is not importable.

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

[
    {
        "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"
            ]
        ]
    }
]

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

[
    {
        "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"
            ]
        ]
    }
]

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

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.

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.

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

1 Like