Possibility of advanced scheduling / cron: bi-weekly

Hey everyone. I know cron in and of itself does not do something such as every two weeks (at least according to my simple google searches) … though is there a way do have a workflow run every two weeks?

Hey @cklph, Welcome to the n8n community!

While not exactly what you are looking for, what I have done with CRON jobs in the past to get this to work is the following:

  1. Schedule a CRON job to run every week
  2. Get the script to determine if the week of the year is an odd or even numbered week
  3. On even numbered weeks, run the next piece of code
  4. On odd numbered weeks, cancel running any further lines of code
1 Like

Thanks a lot @Tephlon! Yes agree that is also the best I could think of. Here an example how it would work in n8n:

{
  "nodes": [
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyWeek"
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        350,
        650
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$node[\"Function\"].json[\"week\"]%2}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        750,
        650
      ]
    },
    {
      "parameters": {
        "functionCode": "// Week calculation code from:\n// https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php\nlet now = new Date();\nlet onejan = new Date(now.getFullYear(), 0, 1);\nitems[0].json.week = Math.ceil( (((now - onejan) / 86400000) + onejan.getDay() + 1) / 7 );\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        650
      ]
    },
    {
      "parameters": {},
      "name": "Whatever should run",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        950,
        550
      ]
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "Whatever should run",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes