Test date in switch


my problem is, that i need to create some tests for a workflow that uses the current date to switch to different flows.

Please share your workflow

I need to test an incoming date in the switch if it is 30, 60, 90 or more than 100 days old - compared to today.

The switch expression is simple as i compare the incoming date with a JS expression like this:

// compare with 30 days
{{ $today.minus({days:30}).toFormat("dd.MM.yyyy") }}

For the tests, i need to create testdata. How is i replace the $today expression with fixed test data or ‘override’ the value of this variable.

I am unsure how to create tests with testdata to ensure that complex workflows work as expected.

Hi @rafi, welcome to the community :tada:

How is i replace the $today expression with fixed test data or ‘override’ the value of this variable.

You could use DateTime.fromFormat() to read a fixed value, for example like so:

{{ DateTime.fromFormat("15.04.2023", "dd.MM.yyyy").minus({days:30}).toFormat("dd.MM.yyyy") }}


Instead of my example value "15.04.2023", you could also use expression to reference incoming data as needed in case you need to make this dynamic. For example, if your date is coming through in a field named myDate, you could read it using an expression like this:

{{ DateTime.fromFormat($json["myDate"], "dd.MM.yyyy").minus({days:30}).toFormat("dd.MM.yyyy") }}

However, I am not quite sure how you are using this expression in a Switch node since it doesn’t return the actual duration between two dates but a specific string. Perhaps you want to calculate an actual duration instead? In a workflow, this could look like so:

This example would calculate the days between today and whatever date you’re passing on to your switch node, then use the result for the routing decision.

Hope this helps! Let me know if you have any questions on the above :slight_smile:

1 Like

thx for answering!

I tried it like this:

on the left data site, you can see the date format. This is a testdate but in the right format.
Now i need to test this testdate not against the real value of $today but against some other test date to simulate all switch case blocks easily.

How can i change the $today value so that i can test this configuration of nodes with testdata. I dont want to change the node configuration to test - just the values.

You could simply replace $today with DateTime.fromFormat("15.04.2023", "dd.MM.yyyy") if you’d like to test the 15th of April 2023 for example. This will work with any other test date as well of course :slight_smile:

good morning. Sure i can do that.

What i really mean is to keep the node configuration as is and create a test environement.
So i dont need to change the application (or the node configuration in this case) after i have tested the workflow.

For example: For the HTTP request node, i was able to replace the request URL variable in the .env file. So the app uses requests to localhost instead of the server that we need in production mode:

So i am looking for the way its ment to be to test large or complicated workflows

If you want to read your test date dynamically from an environment variable you could use the very same approach. Assuming your variable is called TEST_DATE and the format is dd.MM.yyyy, you’d need to replace $today with DateTime.fromFormat($env["TEST_DATE"], "dd.MM.yyyy") here.

ok - i see. thx for the help. I will find a solution now!

The command let parsedDate = DateTime.fromISO($json.latestDate) that i use now here returns an invalid date:

this is the output of the console.log where the error appears the zone "'Europe/Berlin'" is not supported

Node: "Code"] {ts: 1683795344294, _zone: {…}, loc: {…}, invalid: {…}, weekData: null, …}c: nullinvalid: {reason: 'unsupported zone', explanation: `the zone "'Europe/Berlin'" is not supported`}isLuxonDateTime: trueloc: {locale: 'en-US', numberingSystem: null, outputCalendar: null, intl: 'en-US', weekdaysCache: {…}, …}o: nullts: 1683795344294weekData: null_zone: {zoneName: "'Europe/Berlin'", valid: false}[[Prototype]]: Object

what is wrong here?

Hi @rafi, I’d suggesting using the Set node to use such expressions, that’d be much easier here.

If you want to use the Code instead you’d need to make sure to keep the n8n data structure in mind.

Here’s an example workflow comparing the options:

Using the Code node in “Run Once for All Items” mode the result would look like so:

1 Like

yeah! this works

thx a lot for helping out!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.