[Shopify]Liquidで日付を加算して何日後の計算を実現する方法

[Shopify]Liquidで日付を加算して何日後の計算を実現する方法

This time, we will show you how to add the date in the date output on Liquid to achieve ____ days later.

For example, when a date is specified, the date and time of arrival to the customer and the date and time of shipment are different. In such a situation, it would be useful to calculate the shipping date based on the date and time specified by the customer.

Although the use of this function is limited, I hope it will be useful to someone.

How to output dates with Liquid

The first step is to output the date in Liquid. In this case, we will use the date when the product was created as a reference.

{{ product.created_at | date: "%Y-%m-%d" }}

The output will be something like "2022-09-30".

The following article summarizes the output method after this date, if you would like to refer to it.

[Shopify] Summary of Date Output Formats in Liquid

How to add dates and calculate the number of days later in Liquid

Next, let's move on to the main topic, "how to output the number of days after a specific date. The process is as follows

  1. Convert a specific date to seconds
  2. Convert the number of days after ____ days into seconds
  3. Add the above two
  4. Convert back to normal date from seconds conversion

This is the flow of the process.

If we write it in Liquid code, it will look like this

{% assign todayDateSecond = "now" | date "%s"
%}{% assign daysInSeconds = 7 | times: 24 | times: 60 | times: 60

%}{% assign futureDate = todayDateSecond | plus: daysInSeconds
%}{{ futureDate | date: "%Y-%d-%d" }}



The date 7 days later can be obtained in the following way. Incidentally, "now" is used to obtain the current date.

Let me explain line by line...

First, convert the current date "now" to seconds.

{% assign todayDateSecond = "now" | date "%s" %}

(2) Convert 7 days to seconds.

7 days x 24 hours x 60 minutes x 60 seconds = seconds in 7 days

{% assign daysInSeconds = 7 | times: 24 | times: 60 | times: 60 %}

(3) Add ① and ② (at this point, still displaying seconds)

{% assign futureDate = todayDateSecond | plus: daysInSeconds %}

(4) Change the number of seconds to date display and output

{{ futureDate | date: "%Y-%d-%d" }}

This is the flow of the process.

Now you can calculate the date equivalent in days in your Shopify store. The seconds are very useful. If you want to do date conversions or calculations in Liquid, you may want to remember to try the seconds conversion for now.

By the way, the method of date comparison in Liquid is also summarized below, if you want to know how to do it.

[Shopify] How to get and compare dates with Liquid

But if you've read this far, you can already compare dates, right?

How to add dates in [Shopify]Liquid to calculate days later Conclusion

This is how to get the number of days after a specific date in Shopify. By the way, if you can get the number of days after, you can also get the number of days before.

{% assign futureDate = todayDateSecond | minus: daysInSeconds %}

The calculation can easily be done by setting the PLUS location to MINUS in the form "minus".

Date-related operations are often used to build Shopify stores, so it is one of the most important points to keep in mind.

Thank you very much for reading this article! Have a great day!

Contact form

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