Jinja snippets - Date manipulation

By default, Jinja does not have filters to manipulate with date types. The Certainly platform has a few custom Jinja filters to work with date types. To learn more about the Jinja template language refer to the Technical tutorial for using Jinja2.

Date Time Filter

This filter will give you the actual date and time at the moment when it is used. The format is:

{{"%d-%m-%Y %H:%M"| date_time(timeZone, timeDelta)}}
Details:
  • "%d-%m-%YT%H:%M:%S" - can be modified to display the time how you want it
    • d  = days
    • Y = year
    • m = months
    • H = hours
    • M = minutes
    • S = seconds
      You can arrange or remove any of them as you wish, and "-" can also be changed to / or any other delimiter.
  • timeZone - the time zone you want to use. Example: 'Europe/Madrid'
  • timeDelta - represents the number of days ahead or behind the current date. Example: 3
    • A number of days to add (or subtract) to the current date.
    • It also accepts a float number if you need a time shift of less than a day. For example, -0.041 6 will result in an hour before (1/24 = 0.0416).
    • 4 will move the date ahead by 4 days
    • -4 will move the date behind by 4 days

Examples of format:

Jinja Template Output
{{ "%H:%M" | date_time }} 13:49
{{"%d-%m-%Y" | date_time}} 23-12-2019
{{ "%d-%m-%Y %H:%M"| date_time }} 23-12-2019 13:49
{{"%d-%m-%Y %H:%M"| date_time('Europe/Madrid')}} 23-12-2019 13:49
{{"%d-%m-%Y %H:%M"| date_time('Europe/Madrid', 3)}} 26-12-2019 13:49

You can find a full list of date format values in the Python documentation.

One example of use would be a connection inside the bot where you check for opening hours:mceclip0.png

Taking weekdays into account:

If you need to include the day of the week in your schedule of open hours, you can do so with: 

{%- set day = "%w" | date_time("Europe/Amsterdam") | int -%}

Note that the weekday is returned as a decimal number, where 0=Sunday, 1=Monday... 6=Saturday.

So, consider we need to check opening hours for this example schedule:

Mon(1) - Thu(4): 09:00 - 18:00
Fri(5): 09:00 - 16:00
Sat(6), Sun(0): Closed

We can use the following Jinja code in the Module's Connection:

{%- set time = "%H%M"| date_time("Europe/Amsterdam")|int -%}
{%- set day = "%w"| date_time("Europe/Amsterdam")|int -%}
{%- if ((900<=time<=1800 and 1<=day<=4) or (900<=time<=1600 and day==5)) -%}
Open
{%- else -%}
Closed
{%- endif -%}

Time to Unix Filter

In some cases, it's helpful to compare two dates in order to determine, for example, if we should display content or if the visitor applies for a condition of a benefit, warranty, or discount.

To do so, we would rely on our new tool, the time_to_unix filter:

{{"2022-09-08T09:44:24" | time_to_unix('%Y-%m-%dT%H:%M:%S', 'Europe/Madrid') }}
Details:
  • It returns a number in milliseconds passed since Jan 1st, 1970 (0).
  • It will receive a string with a time format and return the unix value for that timestamp. The filter will accept 2 params:
    • "%d-%m-%YT%H:%M:%S" - can be modified to display the time how you want it
      • d  = days
      • Y = year
      • m = months
      • H = hours
      • M = minutes
      • S = seconds
        You can arrange or remove any of them as you wish, and "-" can also be changed to / or any other delimiter. Note: need to match format inside filter param.
    • timeZone - (optional) the time zone you want to use. Example: 'Europe/Madrid'

Examples of format:

Jinja Template Output
{{ "2022-09-01" | time_to_unix('%Y-%m-%d') }}

1661983200000.0

{{"01/11/2021T05" | time_to_unix('%d/%m/%YT%H','Europe/Madrid'}}

1635739200000.0

{{"2022-09-01T10:50:32"| time_to_unix('%Y-%m-%dT%H:%M:%S', 'Europe/Madrid')}}

1662022232000.0

Having the date in unix format allows us to compare the difference between a specified date and the current date without applying complex conditions. For instance, to calculate the difference in days between the order date and today:

{%- set current_date="unix"|date_time -%} 
//"1662649356920.472"
{%- set order_date = "2022-08-26"|time_to_unix('%Y-%m-%d', 'Europe/Madrid') -%}
//"1661464800000.000"
{%- set difference_days = (current_date - order_date)/1000/60/60/24 -%}
//13.721159084455701

In this example, we do the following:

  1. Get the current date in unix format (you can apply timezone and delta to that one as well).
  2. Use the new filter to pass the order date as a string and save it in a variable.
  3. Divide the result of current_date minus order_date by 1000 to convert it to seconds, by 60 to convert it to minutes, by 60 to convert it to hours, and then by 24 to convert it to days, all to get the difference between "Thu Sep 08 2022 17:02:36 GMT+0200 (Central European Summer Time)" and"Fri Aug 26 2022 00:00:00 GMT+0200 (Central European Summer Time)".

We could base a condition for returns or warranty based on the difference_days result inside our connections and Webhooks in order to determine a particular flow or action.