How to Format Date Variables in Templates

read
Last updated at:

Populate Current Day, Week, Month, or Year

Date tags give you a quick way to insert the time of a campaign into an email. The date is in the time zone of your account. 

  • Current Day of Month: The current day of the month is {% current_day %}
    The current day of the month is 5

  • Current Day of the Week: The current day of the week is {% current_weekday %}
    The current day of the week is Friday

  • Current Month: The current month is {% current_month_name %}
    The current month is September

  • Current Year: The current year is {% current_year %}
    The current year is 2021

Currently, we only support English names for months and days of the week.

Populate Date as Event Variable in Flow Email

Let's say you are sending a thank you email through a flow that triggers whenever a customer places an order. You may want to add a sentence that says, "Thank you for your order on _____" and specify the date of the placed order.

If you look at the data Klaviyo receives along with an event, like a Placed Order event, you should be able to find a variable there that represents the order date. This variable might not be labeled "order date," but there is likely a variable that represents the timestamp of the placed order. This will vary depending on your integration.

Once you find this variable, you'll probably next notice that the format of this date is not ideal to use in a template -- it's a UTC timestamp that doesn't look great in an email, for example:

658465

If you want to populate this date in a more customer-friendly format, you will need to apply a few filters. Klaviyo supports most of the filters used by the Django template language. For this use case, you will need to use the following filters:

  • Format_date_string 
    This filter parses and converts the string of characters sliced from the full UTC timestamp to an actual date; this is necessary so that you can use the date filter to format it
  • Date
    This is where you get to choose how you want your date formatted; Django has a chart that outlines how to approach this

To apply these filters, separate them with a pipe (|) and no spaces in between:

{{ your_variable|format_date_string|date:'F d, o' }}

The above would turn this:

2016-02-11T16:46:08-05:00

into this: 

February 11, 2016

Below are some additional common date formats, along with the formatting used to display them. 

February 26, 2016 

{{ your_variable|format_date_string|date:'F d, o' }}

26 February 2016

{{ your_variable|format_date_string|date:'d F o' }}

02-26-2016

{{ your_variable|format_date_string|date:'m-d-Y' }}

26-02-2016

{{ your_variable|format_date_string|date:'d-m-Y' }}

2/26/16 (no leading 0s)

{{ your_variable|format_date_string|date:'n/j/y' }}

26/2/16 (no leading 0s)

{{ your_variable|format_date_string|date:'j/n/y' }}

Feb 11

{{ your_variable|format_date_string|date:'M d' }}

11 Feb

{{ your_variable|format_date_string|date:'d M' }}

For a full list of date format options, reference Django’s date format documentation

Using the “today” variable

The today variable allows you to display today’s date at the time a message is sent. To display today’s date, use this code: 

{% today "%Y-%m-%d" as today %} {{ today }}

The date will display in this format: 2021-03-18

To apply different formatting, apply the filters from the section above to the {{ today }} variable. For example, {% today '%Y-%m-%d' as today %} {{ today|format_date_string|date:'m/d/Y' }} would render using the format MM/DD/YYYY. 

Calculating a future date

If you’d like to display a future date relative to the day a message is sent, apply the days_later filter to the today variable outlined above, like this: 

{% today "%Y-%m-%d" as today %} {{ today|days_later:5 }}

This variable will display the date five days after a message is sent. So, if the message was sent on March 18, the date displayed would be 2021-03-23. 

This filter can be combined with the formatting filters outlined above to use a different date format. Take this code as an example: 

{% today '%Y-%m-%d' as today %} {{ today|days_later:15|format_date_string|date:'M d' }}

If the message was sent on March 18, this would render as Mar 23. 

Additional Resources

x
Was this article helpful?
30 out of 53 found this helpful