Format Date Variables in Templates

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 2017

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 three filters:
  • Slice
    This assumes you don't want to include the time of the order, just the date; to achieve this, we have to "slice" the full UTC date so that only the first half remains
  • Datetime_from_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, you will separate them with a pipe (|) and no spaces in between:

{{ your_variable|slice:'10'|datetime_from_string|date:'F d, o' }}

The above would turn this:

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

into this: 

February 10, 2016

Django's date filter takes certain characters that all produce different outputs. For example:

  • F d, o
  • February 10, 2016
  • D d M Y
  • Wed 09 Jan2008
  • SHORT_DATE_FORMAT
  • 09/01/2008 

Learn more

To learn more about all the date formatting options available, check out Django's page on date filters.
Was this article helpful?
1 out of 1 found this helpful