How compare date format with "IF/Filter" nodes?

Describe the problem/error/question

Hi, there!
stuck while comparing date. I’m using IF or FILTER nodes and get error.

if i choose to compare in string format it works, but string format can’t

What is the error message (if any)?

Comparison type expects a dateTime but both fields are a string use " is after or equal to" operation

Please share your workflow

Input (value 1): formattedDate from json as string, convert to date format
{{ DateTime.fromFormat($json.formattedDate, “dd-MM-yyyy”).toFormat(“dd-MM-yyyy”) }}

value 2: {{ $now.toFormat(‘dd-MM-yyyy’) }}

Operation: is after or equal to

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.91.3
  • Running n8n via Docker
  • Operating system:

toFormat() will convert the date objects to strings, so in your IF node you need to compare as strings, not dates. You other option is to use the date compare node

Hi @Karmanoid

I don’t think this is supported. Instead, try using fromISO()

it’s quite strange, because

and in cloud version i don’t have such issue, i’ve just copied workflow to self-hosted

about fromISO() i’ve got an answer from n8n Assistant )

To convert a string in the format dd-MM-yyyy (for example, “23-06-2019”) to a date in n8n, you should use Luxon’s fromFormat() function, not fromISO(). The fromISO() function is only for ISO 8601 date strings (like “2019-06-23T00:00:00.00”).

Here’s how you can do it in an n8n expression:

{{ DateTime.fromFormat(“23-06-2019”, “dd-MM-yyyy”) }}

Replace “23-06-2019” with your date string variable or field.

Summary:

  • Use fromISO() only for ISO 8601 strings.
  • For “dd-MM-yyyy” format, use fromFormat(dateString, “dd-MM-yyyy”).

@Karmanoid Thanks for the screenshot, that’s really helpful.

You’re absolutely right about Luxon I completely forgot about that.

So, if you need to compare two values as DateTime objects (not strings like in the screenshot), try using the following expressions:

{{ DateTime.fromFormat($json.formattedDate, "dd-MM-yyyy") }}
{{ DateTime.fromFormat($now.toFormat('dd-MM-yyyy'), 'dd-MM-yyyy') }}

These should return proper DateTime values instead of strings,
The output should look like this (DateTime type):

Give it a try on your end and let me know how it goes.

1 Like

@mohamed3nan it works! ) thnx a lot :handshake:

2 Likes

Glad this is resolved now :slight_smile: nice solution @mohamed3nan

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