Subtract one month form a date as string (received via webhook)

Hi all, I receive the following date 07/12/2021 as string via webhook in a var called {{ $json["Sent"] }}.

I’m using this inside a set node and would like to subtract a month from 07/12/2021 in order to be able to assign then 11/2021 to a specific var (I can discard the day, just need month and year).

Should be something like {{ new Date($json["Sent"]).getMonth() -1 }} but this returns 5:

Any suggestions how to do this? Thanks for any hint;)

i use this for getting the last 7 days from now

{{ new Date(new Date().getTime()-(7*24*60*60*1000)).toISOString()}}
1 Like

Hi @eim, did you have a look at the Date & Time node?

This can perform calculations on the date string you have described and also format it as you have described (as it supports custom formats):

image

Example Workflow
{
  "nodes": [
    {
      "parameters": {
        "value": "={{$json[\"result_date\"]}}",
        "dataPropertyName": "formatted_result_date",
        "custom": true,
        "toFormat": "MM/YYYY",
        "options": {}
      },
      "name": "Date & Time1",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "input_date",
              "value": "07/12/2021"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "action": "calculate",
        "value": "={{$json[\"input_date\"]}}",
        "operation": "subtract",
        "duration": 1,
        "timeUnit": "months",
        "dataPropertyName": "result_date",
        "options": {
          "fromFormat": "DD/MM/YYYY"
        }
      },
      "name": "Date & Time",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "value": "={{$json[\"result_date\"]}}",
        "dataPropertyName": "formatted_result_date",
        "custom": true,
        "toFormat": "MM/YYYY",
        "options": {}
      },
      "name": "Date & Time1",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        900,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Date & Time",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time": {
      "main": [
        [
          {
            "node": "Date & Time1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Works great:

image

Thanks for the support;)