[Shopify] Liquidで日付を取得して比較する方法

[Shopify] Liquidで日付を取得して比較する方法

I was building a Shopify store and had a situation where I needed to compare the current date with another date.

When I had a 'specific time' and a 'current time', I had to compare the current date with the other date.

(1) "Specific date" > "Current date

(2) "Specific date" < "Current date

This method can be used when you want to know whether

However, when doing the above, it is not possible to simply compare the date "2021-09-01" directly with the date "2021-09-12," so I will describe the method below.

Obtaining the date with Liquid

The first method is to get the date in Liquid.

For example, to get the publication date of an article, use the

{{ article.published_at | date: "%Y-%m-%d" }}

The first line of the table is "1" and the second line is "2.

If "%Y-%m-%d" is used for the date, the output will be in a format such as "2021-09-01".

If you have a preferred format, the following site was very helpful.

https://www.strfti.me/

The output can be in a variety of formats, including year, month, day, and time.

The above is the date the article was published, but you can also get other dates by adding the date to the product publication date (product.created_at) or the metafields of the product.

By the way, the way to display the current date is as follows

{{'now' | date: '%Y-%m-%d ' }}

and "now" can be used for output.

How to compare dates with Liquid

Now that we know how to get dates in Liquid, let's move on to how to compare dates.

Date comparisons are 'converted to seconds'!

As mentioned earlier, a date format such as "2021-09-01" is not comparable. But what if we convert the date format "2021-09-01" into the seconds format "1630501704"?

How comparable it would be, don't you think?

So, for example, the date of publication of the article I just mentioned

{{ article.published_at | date: "%Y-%m-%d" }}

instead of using %s (seconds) and stating

{{ article.published_at | date: "%s" }}

The output is output in the following format.

For example, to determine whether today's date is before or after the article's publication date, use

{% assign today_second = 'now' | date: '%s'
%}{% assign article_published_second = article.published_at | date: '%s'
%}{% if today_second > article_published_second %}
(if later than the published date)
{% else %}
(if before the published date)
{% endif %}





The comparison can be made in the form

In this case, it's not a great example, since it's not very often that an article is before the publication date in the first place, but I hope it helps in terms of comparing dates!

How to get and compare dates in Liquid Summary

This is how to compare dates in Liquid. Surprisingly, it took me a while to come up with the idea of comparing dates in terms of seconds, so I wrote this article just as a memo.

Personally, I thought it was relatively easy to write and easy to use, since there are situations where date-based processing is used to control products.

I have also compiled a list of date formats that can be used, if you would like to take a look.

[Shopify]Summary list of formats for date output in Liquid

Well then, thank you for reading to the end! Have a great day!

Contact form

新規ストア構築、開発などのお仕事の依頼・無料相談はこちら