# Date Calculations Liquid Examples

by Christina Moore Updated

## Date Calculations

If you are using any date math in a UI Action (as opposed to a regular Action), you must first use the `to_date` filter. E.g., `{{ date_field | add_days: 3 }}` will produce an error; `{{ date_field | to_date | add_days: 3}}` will work.

Formatting Dates

27 Nov 2017: `{{ created_at | short }}`

November 27, 2017: `{{ created at | long }}`

27 Nov 2017 9:29AM: `{{ created_at | short_time }}`

November 27, 2017 9:29AM: `{{ created_at | long_time }}`

3 months ago: `{{ created_at | when }}`

11/27/2017: `{{ created_at | date: '%m/%d/%Y' }}`

Common Date Math:

``````Add Days: {{ date_field | add_days: 3 }}

Subtract Days: {{ date_field | subtract_days: 3 }}

To Date: {% assign now_date = now | to_date %} {{ now | to_date }}

Subtract Dates (returns a number): {{ date1 | subtract_date: date2 }}

Assign Current Date: {{ now | date: "%y/%m/%d" }}

Assign Current DateTime: {{ now | date: '%Y-%m-%dT%T' }}``````

Useful Date Liquid Filters:

``````{{ add_years }}
{{ subtract_years }}
{{ subtract_months }}

Output the number of days that have elapsed between two DateTime Fields:

``````{% assign started_sec = phase_started | date: "%s"%}
{% assign ended_sec = phase_ended | date: "%s"%}
{% assign total_elapsed_seconds = ended_sec | minus: started_sec%}
{% assign days_elapsed_int = total_elapsed_seconds | divided_by: 86400%}
{% assign days_elapsed_mod = total_elapsed_seconds | modulo: 86400%}
{% assign hours_elapsed_int = days_elapsed_mod | divided_by: 3600%}
{% assign hours_elapsed_mod = days_elapsed_mod | modulo: 3600%}
{% assign minutes_elapsed_int = hours_elapsed_mod | divided_by: 60%}
{% assign minutes_elapsed_mod = hours_elapsed_mod | modulo: 60%}
{{days_elapsed_int}} Days, {{hours_elapsed_int}} Hours, {{minutes_elapsed_int}} Minutes``````

*Where phase_started and phase_ended represent the custom DateTime Field names whose values should be used in the calculation.

Output the number of elapsed business days between two DateTime Fields:

``````{% assign start = phase_started | date: '%s' %}
{% assign end = phase_ended | date: '%s' %}
{% assign days = end | minus: start %}
{% assign leftover = days |  modulo: 86400 %}
{% assign days = days | minus: leftover %}
{% assign days = days |  divided_by: 86400 %}
{% assign first_day = phase_started | date: '%u' %}
{% assign last_day = phase_ended | date: '%u' %}
{% assign diff = first_day | minus: last_day %}
{% if first_day > last_day %}
{% assign days_to_subtract = 7 | minus: diff %}
{% else %}
{% assign days_to_subtract = diff | times: -1 %}
{% endif %}
{% assign days = days | minus: days_to_subtract | plus:1 | round %}
{% assign weeks = days | divided_by: 7 %}
{% assign business_days = weeks | times: 5 %}
{% if last_day > first_day %}
{% assign last_minus_days = last_day  | minus: days_to_subtract %}
{% if last_minus_days > 6 %}
{% assign business_days = business_days | plus: days_to_subtract | minus: 1 %}
{% else %}
{% endif %}
{% else %}
{% if days_to_subtract != 0 %}
{% assign days_greater= days_to_subtract | minus: last_day %}
{% assign days_to_subtract = days_to_subtract | minus: days_greater %}