CASE: how to compare dates

When comparing dates, it’s a good practise to format the dates to a specific date format (check out our overview here). The reason behind this, is that the dates can have a different format from one another, causing any comparison logic to be faulty.

An example would be comparing an input variable with a date from the period drop:

  • input with the date attribute will be formatted as “dd/mm/yyyy” by default (so 14/02/2022)
  • period drop dates are formatted as “yyyy-mm-dd (eg. period.end_date will give 2022-12-31)

As you can see, comparing 14/02/2022 with 2022-12-31 won’t work due to the different date format. Obviously, if you have 2 variables already in the same format by default (eg. comparing 2 input variables with the date attribute), the comparison logic will work.

However, a good Liquid guideline would be to always format a date to the specific date filter %F which converts any date to a yyyy-mm-dd format.

So for our example:

{% comment %}format all dates to the same date format so compare logic can be executed{% endcomment %}
{% assign date_gm = custom.report_gm.date_report | date:"%F" %}
{% assign end_date_period = period.end_date | date:"%F" %}

{% comment %}create warning when date repport is BEFORE the actual end date of a period{% endcomment %}
{% if date_gm <= end_date_period %}
{% ic %}{::warningtext}
The date of the report General Meeting can not be set before the enddate of the period
{:/warningtext}{% endic %}
{% endif %}

More info can be found here as well.