Useful code snippets

Add days to the order creation date

If you want to calculate a delivery date by adding a certain number of days to date when an order was placed, then you need to use the following trick.

{%- assign three_days_seconds = 3 | times: 24 | times: 60 | times: 60 %}
<DeliveryDate>{{order.created_at | date: "%s" | plus: three_days_seconds | date: "%Y-%m-%d" }}</DeliveryDate>

What does the code do?

The first line calculates the number of seconds contained within 3 days. You might have typed 259200 right away, however, calculating it this way makes it easier to modify to a different number of days, and looks less of a magic number.

The second line converts the order creation date to a Unix timestamp , which is the number of seconds since 1970-01-01 00:00:00, adds the calculated number of seconds within the days, and finally converts the date back to a desired format.