Date and Time Calculations: Adding, Subtracting, and Modifying Time Values

Date & time arithmetic expressions let you calculate, adjust, and compare dates and times dynamically. They’re commonly used to schedule follow-ups, calculate durations, adjust timestamps, and build time-based logic in flows and campaigns—all evaluated at runtime using expressions.

Quick setup checklist

Use this checklist to add, subtract, and compare dates safely inside flows and messages.

  1. Add or subtract time with datetime_add()
  2. Use valid, case-sensitive time units
  3. Change only the time portion with replace_time()
  4. Shift datetimes by hours for follow-ups
  5. Calculate durations with datetime_diff()
  6. Avoid common date & time formatting mistakes
1
Add or subtract time with datetime_add

To add or subtract time from a date or datetime value, use datetime_add.

Syntax: @(datetime_add(date_or_datetime, amount, unit))

Examples:

  • @(datetime_add("2017-01-15", 5, "D")) — adds 5 days to January 15, 2017
  • @(datetime_add(fields.dob, -3, "M")) — subtracts 3 months from a date field

[CAPTURE: Show a message or action field using datetime_add with a date and unit.]

Tip: Use negative values to subtract time.

2
Use valid time units (case-sensitive)

The following units are supported:

  • Y — Years
  • M — Months
  • W — Weeks
  • D — Days
  • h — Hours
  • m — Minutes
  • s — Seconds

Note: Unit values are case-sensitive.

3
Replace the time portion with replace_time

To replace the time portion of a datetime value, use replace_time.

Syntax: @(replace_time(datetime, time))

Examples:

  • @(replace_time(now(), "10:30")) — returns today at 10:30 AM
  • @(replace_time(contact.created_on, time("15:00"))) — keeps the creation date but sets the time to 3:00 PM

[CAPTURE: Show a message preview displaying a date with a replaced time value.]

Important: replace_time does not change the date—only the time.

4
Add hours to an existing datetime

You can use datetime_add to shift a datetime forward or backward in hours.

Example: @(datetime_add(contact.created_on, 15, "h")) — returns the datetime 15 hours after the contact was created.

[CAPTURE: Show a follow-up message scheduled using hour-based datetime arithmetic.]

5
Calculate the difference between two dates with datetime_diff

To calculate the duration between two dates or datetimes, use datetime_diff.

Syntax: @(datetime_diff(date1, date2, unit))

Examples:

  • @(datetime_diff("2017-01-15", "2017-01-17", "D")) — returns 2 days
  • @(datetime_diff("2017-01-15", "2017-05-15", "W")) — returns 17 weeks
  • @(datetime_diff("2017-01-15", "2017-05-15", "M")) — returns 4 months
  • @(datetime_diff("2017-01-17 10:50", "2017-01-17 12:30", "h")) — returns 1 hour
  • @(datetime_diff("2017-01-17", "2015-12-17", "Y")) — returns -2 years

[CAPTURE: Show a calculation result using datetime_diff with different units.]

Important: The result can be negative if the second date is earlier than the first.

6
Use cases and guardrails

Date & time arithmetic is often used to:

  • Calculate age or elapsed time
  • Schedule reminders relative to events
  • Trigger flows after a delay
  • Measure time between actions
  • Format dynamic appointment messages

Important: Common mistakes to avoid:

  • Using invalid or incorrectly-cased units (for example, "d" instead of "D")
  • Forgetting quotation marks around date strings
  • Mixing date formats inconsistently across expressions
  • Expecting replace_time to change the date

Common Issues

My datetime_add() result looks wrong

Cause: The unit is invalid or incorrectly cased.

Fix: Use a supported unit and keep case sensitivity (for example, "D" for days, "h" for hours).

My expression fails when using a date string

Cause: The date string is missing quotation marks or uses inconsistent formatting.

Fix: Wrap date strings in quotes and keep the same date format consistently across your workspace.

replace_time() changed the time, but not the date

Explanation: replace_time only updates the time portion of a datetime.

Fix: Combine datetime_add (to adjust dates) with replace_time (to set the exact time).

datetime_diff() returned a negative number

Explanation: The second date is earlier than the first date.

Fix: Swap the date order or confirm which direction you want the difference to be calculated.