creative designer

Date formatting in Shopify is pretty simple stuff but one common task merchants want to do is the ability to add time to things. Think of the usage case of wanting to show something on a product after a certain amount of time (or conversely hide something).

For starters let's take an existing product and the created_at date and just add a day to it. The code below will just output a new date and nothing more.

{{ product.created_at | date:'%s' | plus:86400 | date:"%F" }}

What we're doing here is converting the date to seconds, adding a day (in seconds), then converting it back to a friendly looking date. This example might output something that looks like this: 2014-04-22

Outputting the date isn't really useful so let's say you want something to happen when the product is a day or more older.

{% assign myDateInSeconds = product.created_at | date:'%s' | times:1 %}
{% assign myDatePlusOneDay = myDateInSeconds | plus:86400 %}
{% assign currentDate = date:'%s' | times:1 %}
{% if currentDate > myDatePlusOneDay %}
This product is less than one day old!
{% endif %}

A word of warning. Since Shopify caches the pages on the site don't expect the time to be real time but there's some tricks that can be done to avoid that.

If you've got a specific date formatting questions just hit me up in the comments below...

Like to work with me?

Let's talk