Liquid Date Formatting and Templates
The Liquid date filter supports the following formatting options:
When using these formatting options you will use date: ahead of your formatting options. This is useful because Date fields are stored in a normalized format of YYYY-MM-DD and times are stored in a normalized military time format. If you want to send a subscriber their date type field in a specific format, in this case hour, minute, meridian indicuatr you would use the following:
{{first_name}}, just a reminder that your appointment is at {{appt_time | date: "%l:%M %p"}} at {{appt_address}}
Here is a similar example in the liquid tester:
Date Formatting Options
Date (Year, Month, Day):
-
%Y: 4 digit year
-
%y: 2 digit year
-
%m: month of the year, zero padded (02, 08, 11)
-
%_m: month of the year, blank padded ( 2, 8, 11)
-
%-m: month of the year, not padded (2,8,11)
-
%B: full month name (January)
-
%^B: uppercased full month name (JANUARY)
-
%b: abbreviated month name (Jan)
-
%^b: abbreviated, uppercased month name (JAN)
-
%d: day of the month, zero-padded (01, 04, 29)
-
%-d: day of the month not padded (1, 4, 29)
-
%e: day of the month, blank-padded ( 1, 4, 29)
-
%j: day of the year (001, 067, 365)
Time Formats (Hour, Minute, Second, Sub-second)
-
%H: hour of the day, 24-hour clock, zero-padded (00,23)
-
%k: hour of the day, 24-hour clock, blank padded (0, 23)
-
%I: hour of the day, 12-hour clock, zero-padded (01, 12)
-
%l: hour of the day, 12-hour clock, blank padded ( 1, 12)
-
%P: meridian indicator, lowercase (am, pm)
-
%P: meridian indicator, uppercase (am, pm)
-
%M: minute of the hour (00, 59)
-
%S: second of the minute (00, 59)
Time Zone:
-
%z - time zone as hour and minute offset from UTC ( +0900)
-
%:z - hour and minute offset from UTC with a colon (+09:00)
-
%::z - hour, minute and second offset from UTC (+09:00:00)
-
%:::z - hour, minute and second offset from UTC (+09, +09:30, +09:30:30)
-
%Z - timezone abbreviation name (UTC, PST)
Weekday:
-
%A: the full weekday name (Sunday)
-
%^A: the full weekday name uppercased (SUNDAY)
-
%a: the abbreviated weekday name (Sun)
-
%^a: the abbreviated weekday name (SUN)
-
%u: day of the week (Monday is 1, 1...7)
-
%w: day of the week (Sunday is 0, 0...6)
Combination:
-
%c: date and time (%a %b %e %T %Y)
-
%D: date (%m/%d/%y)
-
%F: the ISO 8601 date format (%Y-%m-%d)
-
%v: VMS date (%e-%b-%Y)
-
%x: same as %D
-
%X: same as %T
-
%r: 12-hour time (%I:%M:%S %p)
-
%R: 24-hour time (%H:%M)
-
%T: 24-hour time (%H:%M:%S)
-
%+: date(1) (%a %b %e %H:%M:%S %Z %Y)
ISO 8601 week-based year and week number:
The week 1 of YYYY starts with a Monday and includes YYYY-01-04. The days in the year before the first week are in the last week of the previous year.
-
%G: the week-based year
-
%g: the last 2 digits of the week-based year (00..99)
-
%V: week number of the week-based year (01..53)
Week number:
The week 1 of YYYY starts with a Sunday or Monday (according to %U or %W). The days in the year before the first week are in week 0.
-
%U: week number of the year. The week starts with Sunday. (00..53)
-
%W: week number of the year. The week starts with Monday. (00..53)
Seconds since the Unix Epoch:
-
%s: number of seconds since 1970-01-01 00:00:00 UTC.
-
%Q: number of microseconds since 1970-01-01 00:00:00 UTC.
Literal string:
-
%n: Newline character (\n)
-
%t: Tab character (\t)
-
%%: Literal ``%'' character
Relative Date Options
You can use templates with relative dates to send different messages to subscribers based on when a date is. Works in conjunction with date type fields.
-
{% if custom_date_field is_before '2016-07-04' %} Your date is before!{% endif %}
-
{% if custom_date_field is_after '2016-07-04' %} Your date is after! {% endif %}
-
{% if custom_date_field is_same_day_as '2016-07-04' %}Your date is the same day! {% endif %}
Relative date options include:
-
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday
-
January, February, March, April, May, June, July, August, September, October, November, December
-
Winter, Spring, Summer, Fall
-
Yesterday, Today, Tomorrow
-
Last Week, Next Week
-
This Tuesday (or any day of the week)
-
mon 2:35
-
this month, next month
-
last winter
Working with Dates
Check if a date is greater than 14 days away. Days are calculated using seconds: 14 days * 24 hours * 60 minutes * 60 seconds = 1209600
{% assign today = 'now' | date: '%s' %}
{% assign fourteen_days = 1209600 %}
{% assign fourteen_days_from_now = fourteen_days | plus: today | date: "%Y-%m-%d" %}
{% if my_date is_after fourteen_days_from_now %}
Your date is after 14 days from now.
{% else %}
Your date is before 14 days from now.
{% endif %}
Check if we're currently outside business hours:
{% assign hour = 'now' | date: '%H' | plus: 0 %}
{% if hour > 8 and hour < 18 %}
We're in the office!
{% else %}
We're closed.
{% endif %}
Ask for birthdate and have liquid responses based on age. Here are the steps this chunk of Liquid is taking to make this assessment:
-
Capture the Current time and convert to seconds.
-
Convert the birth_date field into seconds.
-
Assign age based on dividing that by the amount of seconds in a year and comparing that against the time it is now.
-
Determine if that age is between 14 and 28.
-
Send one message, "Great, anyone between the ages of 14 and 28 can register!" to those subscribers between the ages of 14 and 28.
-
Send a different message, "You must be between 14 - 28 to register - sorry!" to those subscribers who are not in that age range.
{% capture time_now %}{{ 'now' | date: '%s' }}{% endcapture %}{% capture time_dob %}{{ birth_date | date: '%s' }}{% endcapture %}{% assign age = time_now | minus: time_dob | divided_by: 31536000 %} {% if age >= 14 and age <= 28 %} Great, anyone between the ages of 14 and 28 can register! {% else %} You must be between 14-28 to register – sorry! {% endif %}
Here we've tested with a birth date (custom field name - birth_date) of 4/21/1918. Definitely not between 14 and 28!