Comparing dates in Dynamics 365 Portals using liquid

Introduction
In this article, we are going to discuss how we can compare two dates using liquid in Dynamics 365 portal web template.

If you are new to Dynamics 365 portals, Please refer our earlier article for setting up Dynamics 365 trial  and you can refer this KB to provision portal in your trial.

Requirement
Let’s say we have one customer appointment entity where we have an appointment record for the customer. We want to check if the customer appointment date is a future date or not so that we can do further validation on the portal web page.

Solution
First, we are going to use FetchXML in our Web Template where will get customer appointment record based on the query string like following.

{% comment %} Get Customer appointment based on the query string parameter{% endcomment %}
{% fetchxml customerappointments %}
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="him_customerappointment">
    <attribute name="him_customerappointmentid" />
    <attribute name="him_name" />
    <attribute name="him_customer" />
    <attribute name="him_appointmentdate" />
    <filter type="and">
      <condition attribute="him_customer" operator="eq" uitype="account" value="{{request.params['id']}}" />
    </filter>
  </entity>
</fetch>
{% endfetchxml %}

Above code will get service appointment record for us based on the customer id from the query string parameter. Now we need to get an appointment date from the FetchXML result. While getting an appointment date we will be formatting date using “%y%M%d” combination, you can see liquid date format options here. We will also convert the value to int for easy comparison. You can check complete list here for working with the different data type filters. While working with above format I faced issue for the padding 0, so in case of day is < 10 it was returning single number, for example if today is 2, it returned 2 instead of 02, to get two digit day and month I used "%y%0M%0d" option if both month and day is <10. So to get today days with two digit numbers I used following code

 {% assign var_todaymonth = now | date: "%M"  | integer %}
 {% assign var_todayday = now | date: "%d"  | integer %}

 {% if var_todayday < 10 and var_todaymonth < 10 %}
     {%  assign var_today = now | date: "%y%0M%0d" | integer %}

 {% elsif var_todayday < 10 and var_todaymonth >= 10 %}
   {%  assign var_today = now | date: "%y%M%0d" | integer %}

 {% elsif var_todayday >= 10 and var_todaymonth < 10 %}
   {%  assign var_today = now | date: "%y%0M%d" | integer %}
{% else %}
   {%  assign var_today = now | date: "%y%M%d" | integer %}
 {% endif %}

Further, above code can be used to get customer appointment date as well in the same format and finally we can use the following code to compare the date with the current date to check if the appointment date is in the future or not.

{% if customerappointments.results.entities.size > 0 %}
    {% assign customerappointmentsResult = customerappointments.results.entities[0] %}
    
    {%  assign var_appointmentMonth = customerappointmentsResult.him_appointmentdate | date: "%M" | integer %}
    {% assign var_appointmentDay = customerappointmentsResult.him_appointmentdate | date: "%d"  | integer %}

 {% if var_appointmentDay < 10 and var_appointmentMonth < 10 %}

     {%  assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%0M%0d" | integer %}

 {% elsif var_appointmentDay < 10 and var_appointmentMonth >= 10 %}
  
   {%  assign var_appointmentDate =  customerappointmentsResult.him_appointmentdate | date: "%y%M%0d" | integer %}

 {% elsif var_appointmentDay >= 10 and var_appointmentMonth < 10 %}
 
   {%  assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%0M%d" | integer %}
  
{% else %}

   {%  assign var_appointmentDate = customerappointmentsResult.him_appointmentdate | date: "%y%M%d" | integer %}

{% endif %}
   

    {% if var_appointmentDate > var_today %}
        {% assign var_isappointmentDateInfuture = 1 %}
    {% else %}
        {% assign var_isappointmentDateInfuture = 0 %}
   {% endif %}
{% endif %}

Hope it will help someone !!

4 thoughts on “Comparing dates in Dynamics 365 Portals using liquid

  1. Pingback: Comparing dates in Dynamics 365 Portals using liquid - Microsoft Dynamics CRM Community

  2. Jim Novak

    Very cool idea! I struggled with comparing dates from CDS converted using the %s filter and it simply would not work, so this is a nice solution. I suggest taking one step further with a template that you can call by name.
    For example, create a template called “Date As Integer” and add an additional variable that you can pass as a named param. Once you include the value in your template, capture the result into a new assign –

    Template – Date As Integer

    {% comment %} Capture the in param of dateToConvert {% endcomment %}
    {% assign thisDate = dateToConvert %}

    {% comment %} grab some date parts for formatting with zeros {% endcomment %}
    {% assign month = thisDate | date: “%M” | integer %}
    {% assign day = thisDate | date: “%d” | integer %}

    {% comment %} formatting with zeros on day / month {% endcomment %}
    {% if day < 10 and month < 10 %}
    {% assign dateAsInt = thisDate | date: "%y%0M%0d" | integer %}
    {% elsif day = 10 %}
    {% assign dateAsInt = thisDate | date: “%y%M%0d” | integer %}
    {% elsif day >= 10 and month < 10 %}
    {% assign dateAsInt = thisDate | date: "%y%0M%d" | integer %}
    {% else %}
    {% assign dateAsInt = thisDate | date: "%y%M%d" | integer %}
    {% endif %}

    {{dateAsInt}} can be accessed by the calling template…

    Call it like this, example of converting 'now'
    {% include 'Date As Integer' dateToConvert:'now' %}
    {% assign nowdate = dateAsInt %}

    Now I can call this with 2 lines!

    Thanks again for the solution!

    Reply
  3. Pingback: Compare Dates in Liquid Web Template (Liquid Script) in Powerapps/Microsoft 365 Portals - Microsoft Dynamics CRM Forum Community Forum

Leave a Reply

Your email address will not be published. Required fields are marked *