You can build a flow to calculate the years since an important date, such as an anniversary, when one of your customers opened an account, or another important milestone.
In this tutorial, you'll create a new flow that calculates the years since Postman's founders founded the company. However, feel free to use your own milestone instead.
You build flows in a workspace. Workspaces let you organize your API projects and collaborate with your team.
To create a new flow, do the following:
When you run a flow, the Now block gives you the current date and time, which you'll need to calculate the years since the milestone.
When you connect two blocks, you connect one block's input to another block's output. Inputs are on the block's left side and outputs are on its right side.
It's optional to connect a Create Variable block. However, if you plan to use a value in multiple places, the best practice is to create a variable. These steps will also let you separate your logic.
To connect a Create Variable block, do the following:
In this tutorial, you'll separate the logic to get the current date and time from the logic to calculate the years since the milestone and other logic. You can group blocks to better separate this logic.
To group your blocks, do the following:
In the following set of procedures, you'll add a Record block to store the milestone date and other milestone-related information.
You could add a Date block instead. However, the Record block resembles the kind of information an API returns to you. In other words, these steps will prepare you to build more complex flows in the future.
Optionally, add more data blocks. For example, to build on the previous steps, you can create a more complex data structure to represent the company's founders. In this case, you could add a list of records, where each item in the list represents a founder, and each record stores the founder's details.
When you finish this tutorial, you'll calculate the difference between now and the milestone date. However, this calculation will return an answer in milliseconds. For this reason, you must calculate millisecond per year and divide by this value to calculate the difference in years.
In this set of procedures, you'll calculate the average milliseconds per year, which takes leap year into account. However, note that while this is a great approximation, there are more exact methods to calculate the exact value.
Decide where on the canvas you want to place the Evaluate block and right-click that location.
Select Evaluate.
Add the following to the code editor:
1000 * 60 * 60 * 24 * 365.25
Once you've created the variables you need to calculate the years since the milestone (now
, date
, and millisecondsPerYear
), you can use them to get an answer.
Decide where on the canvas you want to place the Evaluate block and right-click that location. Then, select Evaluate.
Your Evaluate block will reference all the variables you created: now
, date
, and millisecondsPerYear
.
All Evaluate blocks default to Flows Query Language (FQL). You'll use FQL to calculate the years since the milestone.
To calculate the value, add the following to the code editor:
$floor(
(now - date) /
millisecondsPerYear
)
This calculation takes the difference between now and the milestone date (which is in milliseconds) and divides it by milliseconds per year to get the years since the milestone. The FQL $floor()
function removes any decimal values.
Optionally, use TypeScript to calculate this value. If you'd like to try this option, add another Evaluate block, switch to TypeScript (upper-right corner), and use the TypeScript Math.floor()
method instead.
From the canvas toolbar, select Run.
Congratulations! You calculated the years since a milestone and displayed the result in a Display block.
Last modified: 2024/09/30