Articles in this section

Calculate the number of days between dates

You can use Handlebars date helpers to compare dates and work with date offsets in integrator.io. This article shows how to convert dates to seconds since the epoch, add a fixed offset, and compare two dates, using a 21-day offset as the example. In the examples, myDate is the date you're comparing against.

Convert a day offset to seconds and milliseconds

First, express your offset as a number. 21 days is 1,814,400 seconds (21 × 24 × 60 × 60). The dateAdd helper works in milliseconds, so the same offset is 1,814,400,000 milliseconds.

Building-block expressions

A few helpers do the work:

Current timestamp:

{{timeStamp}}

Add 21 days to a date. The dateAdd helper uses milliseconds:

{{dateAdd myDate 1814400000}}

Current time in seconds since the epoch, using the X format code:

{{dateFormat 'X' timeStamp}}

myDate in seconds since the epoch:

{{dateFormat 'X' myDate}}

myDate plus 21 days, in seconds. The add helper works in seconds here:

{{add 1814400 (dateFormat 'X' myDate)}}

Compare two dates

Combine these expressions with the compare helper to branch on the result. This example checks whether 21 days from now is later than myDate. It converts both the current time (plus 21 days) and myDate to seconds since the epoch, then compares them.

{{#compare (add 1814400 (dateFormat 'X' timeStamp)) ">" (dateFormat 'X' myDate)}}
21 days from now is greater than myDate
{{else}}
21 days from now is less than myDate
{{/compare}}

If 21 days from now is greater than myDate, the first message renders. Otherwise, the {{else}} message renders.

Notes and considerations

  • Use the right unit for each helper: dateAdd works in milliseconds, while dateFormat 'X' and add work in seconds in these examples.