How to persist last run date and use it as start date in a scheduled workflow

I’m using a Schedule Trigger in n8n.

Each run uses:

  • endDate = current trigger date

  • startDate = previous run’s endDate

Example:

  • Run 1: start = 12 Jan, end = 21 Jan

  • Run 2: start = 21 Jan, end = current trigger date

How can I persist the last endDate and reuse it as the next startDate in n8n?

What’s the best approach for this?

Yes, you can do this in n8n and it can be done without using any sort of storage either. You can use “Workflow Static Data” to preserve the data between executions of the workflow. Use a normal Schedule Trigger, then add a code node with the following code:

const staticData = $getWorkflowStaticData('global');  const startDate = staticData.lastEndDate ?? '2024-01-12'; const endDate = new Date().toISOString();  // persist for next run staticData.lastEndDate = endDate;  return [{ startDate, endDate }];   

Complete your logic and use “startDate” and “endDate” for the rest of your workflow. Just to be safe you might want to add another Code node at the end to update the “lastEndDate”. This should help and accomplish what you are looking for.