Node - remove days from date

Hello everyone, it is a pleasure to start on n8n which is amazing and I would like to take a question.
I will receive a webhook with a future date.

Example:
format DD-MM-YYY

25/01/2020

I would like to always take this value (expression) from the webhook and remove 2 days.
The date I need would be:

23/01/2020

it’s possible? how?

Hi @Alexandre_Magno :wave:, welcome to the community!

You can do this using expressions in n8n. Here’s an example workflow that uses a Set node to stimulate the data coming from your Webhook node and another Set node that removes two days from the date.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "Date",
              "value": "25/01/2020"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Date",
              "value": "={{$json[\"Date\"].split('/')[0]-2}}/{{$json[\"Date\"].split('/')[1]}}/{{$json[\"Date\"].split('/')[2]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Instead of expressions, you can also use the Function node for a better implementation than the one shown in the example above. If you are running a self-hosted version of n8n, you can also use built-in and external modules for easier manipulation of data.

You can find some handy JS snippets to use with n8n here. Hope that helps! :slight_smile:

3 Likes

With [email protected] an addition to the DateTime node got added which makes that now simpler.