Use the Difference between dates action in the Datetime package to calculate the difference between two date and time values and return the result in a selected time unit.
Settings
- Double-click or drag the Difference between dates action from the Datetime node in the Actions palette.
- In the Source date and time variable field, specify a date and time or a datetime variable that holds the source date.
- In the Date and time variable to be compared field,
specify the target date with which you want to compare the source date.Note: If the source date is greater than the target date, then the output is negative.
- In the Time unit field, select one of the following units
in which you want the output to appear.
- Years
- Months
- Weeks
- Days
- Hours
- Minutes
- Seconds
- In the Assign the output to a variable field, select a number variable to store the result.
- Click Save.
Example
In this example, you will create an automation to compare the invoice dates with the current date and calculate the difference between them.
- Create an automation.
- In the navigation menu on the left, click Automation.
A list of available bots and forms is displayed.
- Click Create a bot.
- Enter the bot name: Datetime-difference.
- Click Create and Edit.
- In the navigation menu on the left, click Automation.
- Create the following user-defined variables using the Create variable
(+) icon at the top of the Variables menu.
Variable name Variable type Default value invoiceDate Datetime 4/12/2023 10:45 AM GMT (UTC+0:00) diff-year Number 0 diff-month Number 0 - Double-click or drag the Difference between dates action from the Datetime package in the Actions palette.
- In the Source date and time variable field, enter $invoiceDate$.
- In the Date and time variable to be compared field, enter
$System:Date$.Note: You can use the System: Date variable to get the current date. In this example, the System: Date value is 7/12/2024 10:45 AM GMT (UTC+0:00) in this example.
- In the Time unit field, select Years.
- Double-click or drag another Difference between dates action from the Datetime package in the Actions palette.
- In the Source date and time variable field, enter $invoiceDate$.
- In the Date and time variable to be compared field, enter $System:Date$
- In the Time unit field, select Months.
- Double-click or drag the Message box action from the Actions palette.
- In the Enter the message to display field, enter the
following:
Difference between:
Years: $diff-year.Number:toString$
Month: $diff-month.Number:toString$
- When you run the automation, it calculates the difference between the invoice dates and the
current date, and the results are displayed as follows: