How to loop through dates in node red


You can import the code below;


[
    {
        "id": "cb020599.1c4d48",
        "type": "inject",
        "z": "3c26b805.f73b58",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 260,
        "y": 200,
        "wires": [
            [
                "624dd7f1.8f0988"
            ]
        ]
    },
    {
        "id": "f84398fe.40bfb8",
        "type": "debug",
        "z": "3c26b805.f73b58",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "x": 850,
        "y": 200,
        "wires": []
    },
    {
        "id": "624dd7f1.8f0988",
        "type": "function",
        "z": "3c26b805.f73b58",
        "name": "loop through dates",
        "func": "var start = new Date(\"01/03/2005\"); //MM DD YYYY\nvar end = new Date(\"02/03/2005\");\n\n\nvar loop = new Date(start);\nwhile(loop <= end){\n\n    var month = loop.getUTCMonth() + 1; //months from 1-12\n    var day = loop.getUTCDate();\n    var year = loop.getUTCFullYear();\n\n    var str = year + \"-\" + month + \"-\" + day;\n    msg.payload=str;\n    node.send(msg);\n\n   var newDate = loop.setDate(loop.getDate() + 1);\n   loop = new Date(newDate);\n}\nmsg.payload=\"done\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 510,
        "y": 200,
        "wires": [
            [
                "f84398fe.40bfb8"
            ]
        ]
    }
]

Leave a Comment