Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shopify (liquid): Find number of days between two dates

Tags:

shopify

liquid

I am new to Shopify and .liquid files syntax.

I can get two dates currently:

{% assign product_created_date = product.created_at | date: "%a, %b %d, %y" %}
{% assign current_date = 'now' | date: "%a, %b %d, %y" %}

which gives me the current date and also the date when the product was created.

I want to show the users in the theme, the date since the product was posted.

I've read some liquid filters and did some search but couldn't find out exactly how I would find the days since product was created.

Can we calculate it using purely liquid syntax?

like image 449
Seyong Cho Avatar asked May 20 '16 07:05

Seyong Cho


Video Answer


2 Answers

You can transform your dates to timestamps representing Number of seconds since 1970-01-01 00:00:00 UTC

{% comment %} convert our dates to Number of seconds 
              since 1970-01-01 00:00:00 UTC {% endcomment %}
{% assign dateStart = product.created_at | date: '%s' %}
{% assign nowTimestamp = 'now' | date: '%s' %}

{% comment %} difference in seconds {% endcomment %}
{% assign diffSeconds = nowTimestamp | minus: dateStart %}

{% comment %} difference in days {% endcomment %}
{% assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 %}

<p>difference in days = {{ diffDays }}</p>
like image 109
David Jacquel Avatar answered Oct 19 '22 14:10

David Jacquel


Derived with the help of David's Answer one line snippet - consider any date, for eg. '2020-04-09'

{% assign myVar="now" | date: "%s" %}{{ '2020-04-09' | date: "%s" | minus: myVar | divided_by: 3600 | divided_by: 24 | round }}

You can run and Test it here

like image 41
Shubham Jha Avatar answered Oct 19 '22 12:10

Shubham Jha