How to add data to function node?

How do I add data from an incoming node to be used in a function node?

I feel like I did 95% of the work but can’t figure out the simplest of things any insight would be amazing.

So, as you can see the flow is very simple, take 2 dates and calculate the number of days in between both dates.
at this moment I have the flow working with dummy data in the function node and just can’t figure out how to get the real dates to be placed instead of the dummy data

Ok, this question seems very basic (maybe that is why I couldn’t seem to find the answer in the documentation.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        160,
        380
      ]
    },
    {
      "parameters": {
        "action": "calculate",
        "value": "10/08/2021",
        "duration": 10,
        "dataPropertyName": "date1demo",
        "options": {}
      },
      "name": "Date & Time",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        460,
        220
      ],
      "notes": "date"
    },
    {
      "parameters": {
        "functionCode": "// To set two dates to two variables\nvar date1 = new Date(\"06/22/2019\");\nvar date2 = new Date(\"07/30/2019\");\n\n// To calculate the time difference of two dates\nvar Difference_In_Time = date2.getTime() - date1.getTime();\n\n// To calculate the no. of days between two dates\nvar Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);\n\n//To display the final no. of days (result)\n\nreturn [{\n  json: { Difference_In_Days }\n}]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1040,
        300
      ]
    },
    {
      "parameters": {
        "action": "calculate",
        "value": "10/08/2021",
        "duration": 10,
        "dataPropertyName": "date2demo",
        "options": {}
      },
      "name": "Date & Time1",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        460,
        420
      ],
      "notes": "date"
    },
    {
      "parameters": {
        "mode": "mergeByIndex"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        760,
        320
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Date & Time",
            "type": "main",
            "index": 0
          },
          {
            "node": "Date & Time1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Try this:

{
  "nodes": [
    {
      "parameters": {
        "action": "calculate",
        "value": "10/08/2021",
        "duration": 10,
        "dataPropertyName": "date1demo",
        "options": {}
      },
      "name": "Date & Time",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        -520,
        1700
      ],
      "notes": "date"
    },
    {
      "parameters": {
        "action": "calculate",
        "value": "10/08/2021",
        "duration": 10,
        "dataPropertyName": "date2demo",
        "options": {}
      },
      "name": "Date & Time1",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        -520,
        1900
      ],
      "notes": "date"
    },
    {
      "parameters": {
        "functionCode": "// To set two dates to two variables\nvar date1 = new Date(\"06/22/2019\");\nvar date2 = new Date(\"07/30/2019\");\n\n// To calculate the time difference of two dates\nvar Difference_In_Time = date2.getTime() - date1.getTime();\n\n// To calculate the no. of days between two dates\nvar Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);\n\n//To display the final no. of days (result)\n\nreturn [{\n  json: { \"diff\": Difference_In_Days }\n}]"
      },
      "name": "Function3",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        60,
        1780
      ]
    },
    {
      "parameters": {
        "mode": "mergeByIndex"
      },
      "name": "Merge1",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        -220,
        1800
      ]
    }
  ],
  "connections": {
    "Date & Time": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time1": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge1": {
      "main": [
        [
          {
            "node": "Function3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

Hey @David_Go, you can read incoming data as described in these examples from the Function node documentation. It also supports all expressions (you wouldn’t need the curly braces around them in this case).

Edit: @Shirobachi beat me to it, so please ignore my example below :smiley:

So a simple example populating your variables date1 and date2 using the incoming data from your Merge node could look like so:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "action": "calculate",
        "value": "10/08/2021",
        "duration": 10,
        "dataPropertyName": "date1demo",
        "options": {}
      },
      "name": "Date & Time",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        640,
        180
      ],
      "notes": "date"
    },
    {
      "parameters": {
        "functionCode": "// To set two dates to two variables\nvar date1 = new Date($json[\"date1demo\"]);\nvar date2 = new Date($json[\"date2demo\"]);\n\n// To calculate the time difference of two dates\nvar Difference_In_Time = date2.getTime() - date1.getTime();\n\n// To calculate the no. of days between two dates\nvar Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);\n\n//To display the final no. of days (result)\n\nreturn [{\n  json: { Difference_In_Days }\n}]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1220,
        260
      ]
    },
    {
      "parameters": {
        "action": "calculate",
        "value": "10/08/2021",
        "duration": 10,
        "dataPropertyName": "date2demo",
        "options": {}
      },
      "name": "Date & Time1",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        640,
        380
      ],
      "notes": "date"
    },
    {
      "parameters": {
        "mode": "mergeByIndex"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        940,
        280
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Date & Time",
            "type": "main",
            "index": 0
          },
          {
            "node": "Date & Time1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

It maybe be helpful visualization, but since @MutedJam shared docs I would also suggest read it, it suck of good content!


Be aware that you always need to return one array of object, each object has to have json property what needs to be another object with your data

@David_Go Also you may want use external library to count time like moment or date fns

Have fun :slight_smile:

2 Likes

@MutedJam - Thank you so very much for your help on this, this just opened up a whole new world in n8n for me. Works like a charm !

@Shirobachi Thanks for the helpful insight :slight_smile: and I am totally having a blast with n8n! there is not a day that goes by without n8n and it just seems to be getting better and better each and every update that comes out.

2 Likes