Returning Date Information

I can get the current phonetic month when doing a simple new Date() but for some reason when I try to reference data sent in a webhook, it doesn’t return any data… any thoughts on what’s going on here? The data being sent by the webhook is an ISO date as a string.

const results = []
const date = $node["Webhook"].json.query["date"];
const d = new Date(date);

const month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
const n = month[d.getMonth()];

results.push({json:{n}})

return results

Hey @pford, just tried out the code that you shared and it works correctly for me. Could you perhaps share some more information about how you are sending input to the webhook and the value that you are passing?

Here’s my workflow in case it is helpful for you to debug :slight_smile:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const results = [];\nconst date = $node[\"Webhook\"].json.query[\"date\"];\nconst d = new Date(date);\n\nconst month = new Array();\nmonth[0] = \"January\";\nmonth[1] = \"February\";\nmonth[2] = \"March\";\nmonth[3] = \"April\";\nmonth[4] = \"May\";\nmonth[5] = \"June\";\nmonth[6] = \"July\";\nmonth[7] = \"August\";\nmonth[8] = \"September\";\nmonth[9] = \"October\";\nmonth[10] = \"November\";\nmonth[11] = \"December\";\nconst n = month[d.getMonth()];\n\nresults.push({json:{n}})\n\nreturn results;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "path": "04b7a852-cc78-4d85-b4bc-b83140634306",
        "responseMode": "lastNode",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ],
      "webhookId": "04b7a852-cc78-4d85-b4bc-b83140634306"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

And here’s the GET request that I made using my browser: http://localhost:5678/webhook-test/04b7a852-cc78-4d85-b4bc-b83140634306?date=2011-09-05T14:48:00.000Z (might need edits depending on your workflow/how you are running n8n).

Thanks for testing this for me! I also was able to do it with this version of the ISO timestamp but for some reason the service I’m using is sending the ISO as this: "date": "2021-12-19T23:24:30 00:00" instead of the correct format and I think that may be the issue. I know I can use the Date & Time node to convert this, but I’m not sure what the format specifiers should be to correct the final format that is put into the Function node.

1 Like

Nevermind I got it. The proper date format specifier to get it to work with new Date() is YYYY-MM-DDTHH:mm:ss.SSSZ

Thank you for helping me trouble shoot that @tanay!

1 Like

Yay, glad to hear that it worked out. Have fun! :slight_smile: