In this article, we will discuss how to use the liquid script to print a contact’s date of birth, which is saved in one of the custom fields in the format YYYY-MM-DD, into a simple integer value giving us the age of the contact.


Since the date and time saved in Maropost are in the format YYYY-MM-DD, first we would need to convert the date/time into an integer value. Based on the UNIX Epoch Time which is 1st January 1970, converting any date/time into ‘integer value’ will convert the date/time starting from the ‘Epoch Time’, i.e. 1970-1-1 to the present date. Click here to know more about UNIX Epoch Time


You can also use the UNIX Epoch Time Converter to convert a DateTime Stamp into Seconds with respect to the UNIX Epoch Time.

LInk to the converter - https://www.unixtimestamp.com/index.php


The first step would be to convert the data present in the custom field, which contains the date/time, into seconds.

We will use the script cited below:


{% assign dateStart = {{contact.dob_test}} | date: '%s' %}


Here the variable “dateStart” is being assigned the value present in the custom field {{contact.dob_test}}, which is being converted into seconds using the Liquid script filter “date:’%s’”. This will give us the integer value of the seconds elapsed from or before the UNIX Epoch Time (1st January 1970) till the Date/Time saved in the custom field.

Next, we need to fetch the seconds elapsed from the UNIX Epoch Time till the present date.


{% assign nowTimestamp = 'now' | date: '%s' %}


This is necessary since we need to deduct the value of “dateStart” from the “now Timestamp” to get the exact number of seconds for that person’s age. The next step would be to deduct the 2 values fetched.


{% assign diffSeconds = nowTimestamp | minus: dateStart %}


Great! so we have the contact’s exact age in the form of seconds (integer value). The only steps remaining are to convert the seconds into years to print the contact’s age.

First, we are going to convert the seconds into days. Since there are 3600 seconds in an hour, we are going to divide the value of the variable “diffSeconds” with 3600. In the same script, we are going to convert the value which was fetched after converting “diffSeconds” into hours, and then into Days by dividing the value by 24. The script is cited below:


{% assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 %}


Now, the only thing left is to convert the days into years. Since the variable “diffDays” contains the integer value of the Age in the form of days, we will divide it by 365 to get the result in the form of years. The script is given below:


{% assign Age= diffDays | divided_by: 365 %}


Print the value of the variable “Age” using the script below:


Age is {{ Age }}


Awesome! You have successfully converted and printed a contact’s age from the format YYYY-MM-DD to an Integer Value.


The final script would look like this:

{% assign dateStart = {{contact.dob_test}} | date: '%s' %}

{% assign nowTimestamp = 'now' | date: '%s' %}

{% assign diffSeconds = nowTimestamp | minus: dateStart %}

{% assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 %}

{% assign Age= diffDays | divided_by: 365 %}

Age is {{ Age }}