Power Pages

Date Validation – Comparing Dates in Liquid

In this quick tip, we’ll see how to check whether a given date is earlier than today’s date (or another specific date). I’ll compare a date column from Dataverse to today’s date but this method could just as easily be used to compare 2 date columns from Dataverse.

This is something I found a little tricky when I was getting started with liquid. I expected there would be a datediff function but no, so how do we work around this?

In short, the answer is by transforming the dates into numbers using the following format:
yyyyMMdd and then compare e.g. with less than, greater than, equals to, etc. like in the example below
{% if date_to_check <= expiry_validation_date %}
    {{ date_to_check }}
{% else %}
    <span class="glyphicon glyphicon-alert" aria-hidden="true"></span> {{ date_to_check }}
{% endif %}

Read on if you’re interested in how I arrived at this code…

Please note that this simplified approach is suitable for comparing dates only (times won’t be considered as they’d produce a number too big for liquid to handle)

Transforming values

In liquid, we can use filters to transform values into various different formats (see https://learn.microsoft.com/en-us/power-apps/maker/portals/liquid/liquid-filters#date-filters for the available options).
So, how do we filter? Just type a pipe character | after the value, followed by the name of the filter – in our example, that’s date.
If the filter accepts any parameters, we type a colon and then pass the parameters, like so:
{{ now | date: 'yyyyMMdd' }}

(As you can see, getting the current date and time is nice and easy. We use the now keyword.)

Assign the date to a variable for easy reuse

We’ve got a decent starting point BUT we can’t really ‘do’ anything with the result yet other than output on the page. Let’s fix that by assigning it to a variable…
{% assign current_date = now | date: 'yyyyMMdd' | integer %}
With our filters (transformations), current_date now reads something like so: 20230306

In my example, I want to check the Expiry Date column from my Royalty Agreements table in Dataverse. I’ll give that the same treatment; assign to a variable and transform:
{% assign date_to_check = result.musdyn_expirydate | date: 'yyyyMMdd' | integer %}

Now to compare them…

We can then compare with simple operators like:

Symbol Description
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to

So let’s see how to check whether a date is in the past
{% if date_to_check < current_date %}

Taking this a step further… what if we want to check a date is within a certain period?

Example: is expiry date within the next 30 days?

We’ll start with today’s date and add 30 days. For this we have the very handy date_add_days filter

{% expiry_validation_date = now | date_add_days: 30 %}
{% expiry_validation_date = now | expiry_validation_date | date: 'yyyyMMdd' | integer %}

…and then compare using if
{% if date_to_check >= expiry_validation_date %}

In this example, I’ll output a warning icon for expiry dates falling within the next 30 days:

{% if date_to_check <= expiry_validation_date %}
    {{ date_to_check }}
{% else %}
    <span class="glyphicon glyphicon-alert" aria-hidden="true"></span> {{ date_to_check }}
{% endif %}
We have date_add_days, what about subtracting days? There’s not a specific filter for this. To subtract days, we just need to provide a negative value e.g. {% expiry_validation_date = now | date_add_days: -30 %}
Why sometimes one equals sign and sometimes two? 🤔
one = sign is how we set a value.
== is how we check / compare a value
Note: Capitalisation is important here.
HH means 24 hour time rather than hh for 12 hour time
MM means month whereas mm would be minutes
Franco Musso

You may also like

Leave a reply

Your email address will not be published.

More in Power Pages