Need help creating a filter to check if a date field is on a Sunday

I have a flow that changes a certain date field in the system. It is important however that the resulting date is not a Sunday. So I’m trying to build a secondary flow that checks for this and can update the date further if needed.

I thought I would be able to use a script filter to check something such as

var date = new Date(lead_field_date_that_next_drip_message_will_be_sent_formatted);
var isSunday = date.getDay() === 0;

and filter on that but I keep getting errors. Any tips here would be appreciated!

1 Like

Hey @CarsonRedCliffLabs,
this is a common use-case so thanks for posting this question publicly so that we document the solution for everyone :slightly_smiling_face:

Luckily we have two libraries you can use inside the script filter for handling dates:

  1. date-fns
  2. Moment.js

I will describe the date-fns way of doing this as this library is much nicer than Moment.js :wink:
we will be using the isSunday() function of date-fns:

// Note: this code will not work as it is not an expression
var date = new Date(lead_field_date_that_next_drip_message_will_be_sent_formatted);
var isSunday = date_fns.isSunday(date);

As you are inside a script filter, the code above will not work as it has to be an expression. Let’s rewrite the code to an expression that works inside the script filter:

date_fns.isSunday(new Date(lead_field_date_that_next_drip_message_will_be_sent_formatted))

Now we have a single line of code that evaluates to true or false! :100:

By the way: You should get instant feedback whether you are correctly using the date-fns library by the code editor:

Carson I hope I could solve your problem. Feel free to ask any follow-up questions.

Cheers,
Ben

That worked perfectly. Thank you very much!

1 Like