---
heading: 'Manipulating dates in Shopify Liquid'
title: "Adding/substracting days in Shopify's Liquid template language"
date: '2021-08-29'
lastmod: '2024-11-07'
icon: 'Liquid'
draft: false
summary: "How to manipulate dates (adding, subtracting, and formatting, etc) in Shopify's Liquid template language."
tags: ['liquid', 'shopify', 'date']
---

Adding/substracting days in Shopify's Liquid template language

```liquid:snippet.liquid showLineNumbers
{% assign days = 4 %}

{% assign ms = days | times: 24 | times: 60 | times: 60 %}
{% assign now = 'now' | date: "%s" %}

{% assign today = now | date: "%b %d" %}
{% assign before = now | minus: ms |date: "%b %d" %}
{% assign after = now | plus: ms | date: "%b %d" %}

<div>Today: {{ today }}</div>
<div>{{days}} days before today: {{ before }}</div>
<div>{{days}} days after today: {{ after }}</div>
```

Result:

```html:index.html showLineNumbers
<div>Today: Aug 29</div>
<div>4 days before today: Aug 25</div>
<div>4 days after today: Sep 02</div>
```

### How it works

- First convert `now` to seconds since 1970 with filter `| date: "%s"`.
- Convert `days` range to seconds `x 24 x 60 x 60`
- Use filters `| minus` or `| plus` to get the result and parse back to date format with `date: "%b %d"` filter

The format for Liquid date is the same as [strftime](http://strftime.net/).
