Arrays for Date & Time

Hi!

I have a question about how Date & Time node processing arrays of items.

For example I would like to change date format for OpenWeatherMap 5 day forecast.

When I select one element that works fine
When I select two or more ones that fails:

And how do I get around all the items in the list and change its date format?

The Date & Time Node was mainly planned to convert single values. So using it with an array is saldy not easy. Here an example on how to split it into different items, converting the values and combining them again into a single one:

{
  "nodes": [
    {
      "parameters": {
        "operation": "5DayForecast",
        "cityName": "berlin,de"
      },
      "name": "OpenWeatherMap",
      "type": "n8n-nodes-base.openWeatherMap",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "openWeatherMapApi": "open weather"
      }
    },
    {
      "parameters": {
        "value": "={{$node[\"Split in Items\"].json[\"dt\"]}}",
        "dataPropertyName": "dt_new",
        "options": {}
      },
      "name": "Date & Time",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.list.map(item => { return { json: item }; })"
      },
      "name": "Split in Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const list = items.map(item => item.json);\n\nreturn [\n  {\n    json: {list},\n  }\n];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "OpenWeatherMap": {
      "main": [
        [
          {
            "node": "Split in Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split in Items": {
      "main": [
        [
          {
            "node": "Date & Time",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thank you very much! That is exactly what I needed. I suffered a long time with this problem. Thank you for the detailed explanation!

1 Like