IF node for date & time

Hello! Very new here - I was wondering if anyone knows how I can compare two dates and if they are equal, do x.

At present it seems that you cannot check to see if dates are equal in the same way you can with numbers/strings.

Thanks a lot in advance

Hey @Sarah_Strickland!

Welcome to the community :sparkling_heart:

The IF node currently only checks for before and after for the date. There are two possible solutions to achieve what you want to do:

  1. You can use the String type and then use the Equal operation to compare the values.
  2. You can use the Function node to write your own code for the comparison.

I would use the former approach since it is faster and works as expected. If you need to format the date and time, you can use the Date & Time node.

Hey @harshil1712

Thank you for your kind welcome and quick response!

I agree option 1 sounds the best for me. My issue is that the dates are currently in Date & Time format - would I have to use Function to convert it to a string?

Sarah

I don’t think it would be necessary. When you refer to the date in the Value field, what output do you get? You can also use default JavaScript functions in the Expression Editor.

Screenshot 2021-08-13 at 15.30.11

This expression is coming up as red - does this mean that it won’t work?
Sarah

I am not sure why they show as red but I have found that expressions do show in red, As long as you have a green result at the bottom that matches what you are after all should be good.

Thank you very much!!

Hi

Thanks for this post. I have tried to implement it however my my If is giving me incorrect results.

What I am trying to do:
I am pulling information from a snowflake database and extracting the created_date. I then am converting the date using set to a number in the form YYYYMMDD.
I am then setting todays date in the format of YYYYMMDD.
Then I am trying to use the IF node to compare if these two values are equal or not.

This is my workflow (it is a bit of a spaghetti):

For example in this image the first, third and last rows should not be true:

Looking forward to understand what exactly I am doing incorrect here.

Thanks in advance for the help.

Hey @Surge.Media,

ANy chance you could send over the json output of the node before the if? It will make a bit easier to test with.

Hey @Jon

Thanks for the reply:


{
"id": 302316,
"pga_created_date": "2022-05-19T12:11:16.000Z",
"pga_tournament_name": "2022 PGA Championship",
"date": "20220519",
"createdDateNumber": "20220519"
},
{
"id": 301775,
"pga_created_date": "2022-05-17T17:32:33.000Z",
"pga_tournament_name": "2022 PGA Championship",
"date": "20220517",
"createdDateNumber": "20220517"
},
{
"id": 282866,
"pga_created_date": "2022-05-19T05:16:44.000Z",
"pga_tournament_name": "2022 PGA Championship",
"date": "20220519",
"createdDateNumber": "20220519"
},
{
"id": 303993,
"pga_created_date": "2022-05-11T13:30:42.000Z",
"pga_tournament_name": "2022 Byron Nelson",
"date": "20220511",
"createdDateNumber": "20220511"
},
{
"id": 331423,
"pga_created_date": "2022-05-19T08:58:12.000Z",
"pga_tournament_name": "2022 PGA Championship",
"date": "20220519",
"createdDateNumber": "20220519"
},
{
"id": 337070,
"pga_created_date": "2022-05-19T09:32:58.000Z",
"pga_tournament_name": "2022 PGA Championship",
"date": "20220519",
"createdDateNumber": "20220519"
},
{
"id": 298874,
"pga_created_date": "2022-05-19T11:55:59.000Z",
"pga_tournament_name": "2022 PGA Championship",
"date": "20220519",
"createdDateNumber": "20220519"
},
{
"id": 304601,
"pga_created_date": "2022-05-19T06:47:20.000Z",
"pga_tournament_name": "2022 PGA Championship",
"date": "20220519",
"createdDateNumber": "20220519"
},
{
"id": 280804,
"pga_created_date": "2022-05-19T11:33:22.000Z",
"pga_tournament_name": "2022 PGA Championship",
"date": "20220519",
"createdDateNumber": "20220519"
},
{
"id": 299292,
"pga_created_date": "2022-05-11T17:32:13.000Z",
"pga_tournament_name": "2022 Byron Nelson",
"date": "20220519",
"createdDateNumber": "20220519"
}
]

What i do not understand is why it is not showing the parameted todayDateNumber and rather showing “date”

Hey @Surge.Media,

So the first thing I am seeing without loading that into n8n is those values are strings rather than numbers. If you set your If node to compare strings it may work or you can try using Number() or ParseInt() around the variable to convert it to a number. I suspect that may do the job for you.

Hi @Jon

I have tried with both string and number and still not getting the desired results.

Hey @Surge.Media,

Give this a bash.

1 Like

Hi @Jon

That did the trick. I see in the variable selector instead of calling the $node, you called the $item. I am about to start the second course provided by n8n and hopefully will start to understand this.

For the moment you fixed things and working like a charm. Much appreciated! Have a good weekend.

1 Like