Custom due date in email template / invoice template

Hello all, this is not a question or request - I’m sharing part of what I’ve just built for my email template, in the hope that others will find it useful.

In another post it was explained how to insert the Due Date into an email template:

However, what I wanted was a specific format that is not supported in the Preferences.

Format as per Preferences: 18/08/2017
Format I wanted for invoices: Aug 18, 2017

To achieve this:

Store the due date in a variable:

{% for field in fields %}
  {% if field.label == "Due date" %}
    {% capture due_date %}{{ field.text }}{% endcapture %}
  {% endif %}
{% endfor %}

Split that variable into three, one to represent each part:

{% assign due_date_array = due_date | split: '/' %}
{% assign day = due_date_array[0] %}
{% assign month = due_date_array[1] %}
{% assign year = due_date_array[2] %}

Note: For the above, if you use the American date format (08/18/2017), swap around the words “day” and “month”. Keep the rest the same.

Convert the month into a textual representation:

{% case month %}
  {% when '01' %}
    {% assign month_short = 'Jan' %}
  {% when '02' %}
     {% assign month_short = 'Feb' %}
  {% when '03' %}
     {% assign month_short = 'Mar' %}
  {% when '04' %}
     {% assign month_short = 'Apr' %}
  {% when '05' %}
     {% assign month_short = 'May' %}
  {% when '06' %}
     {% assign month_short = 'Jun' %}
  {% when '07' %}
     {% assign month_short = 'Jul' %}
  {% when '08' %}
     {% assign month_short = 'Aug' %}
  {% when '09' %}
     {% assign month_short = 'Sep' %}
  {% when '10' %}
     {% assign month_short = 'Oct' %}
  {% when '11' %}
     {% assign month_short = 'Nov' %}
  {% when '12' %}
     {% assign month_short = 'Dec' %}
  {% else %}
     {% assign month_short = 'XXX' %}
{% endcase %}

Note: For the above, if you want the entire name for a month (e.g. ‘January’), simply replace each word.

And now, to actually add it into the invoice:

Your invoice is due {{month_short}} {{day}}, {{year}}.

Which renders as:

Your invoice is due Aug 18, 2017.

This should also work for invoice templates, but I haven’t tested it in that scenario.

6 Likes