I’m working with Shopify liquid templates and need help with date calculations. I want to display how many days have passed since a product was added to the store.
Right now I can get both dates like this:
{% assign item_launch_date = product.created_at | date: "%Y-%m-%d" %}
{% assign today_date = 'now' | date: "%Y-%m-%d" %}
This gives me the product creation date and today’s date. But I’m stuck on how to subtract these dates to get the number of days between them.
I want to show customers something like “Added 5 days ago” on the product page. Is there a way to do this math using only liquid filters without any external scripts?
you can use the minus filter, but convert the dates to timestamps first. try this: {% assign days_diff = 'now' | date: '%s' | minus: product.created_at | date: '%s' | divided_by: 86400 %}. just remember 86400 is seconds in a day!
The timestamp approach works well, but I hit timezone issues when I tried it on my store. I had better luck using the date filter with %j format to get the day of year, then doing the math from there. But that gets messy across year boundaries. Here’s what actually worked: {% assign seconds_diff = 'now' | date: '%s' | minus: product.created_at | date: '%s' %} then {% assign days_ago = seconds_diff | divided_by: 86400 %}. Just wrap it in an unless statement so you don’t get negative numbers if the date formatting breaks. This has been rock solid across different timezones for me.