Date related operations

Here are a few coperations you can perform using dateTime variables.

Convert string variables to dateTime

{% assign myDate = "2015-01-07" | convert: "date" %}

This code will create the myDate variable as dateTime.


will render 2015-01-07T00:00:00

Compare dates with the current date

To compare two dates simply treat them like regular numeric variables:

{% if < myDate %}
<p>The date is in the future</p>
{%else %}
<p>The date is in the past</p> {% endif %}

Get the difference between dates

To get the difference between the current date and any given date you can simply substract your custom date from the global variable.

For this example let's assume we need to create our custom date variable:

{% assign myVar = "2015-01-07T15:52:30" | convert: "date" %}

This will create the myVar variable and properly format it as a timeSpan type variable. Now to get the difference in days between this date and the current date:

{% assign diffDays = | minus: myVar | date: "%d" %}

The diffDays timeSpan variable will hold the number of days between the two dates.