How to use the dateDifference() function in Power Automate

Sending
User Review
0 (0 votes)

Power Automate has recently introduced an innovative function aimed at calculating the difference between two dates. However, finding the difference between two dates in Power Automate was quite complex, and with the recent introduction of dateDifference() function, Microsoft has eased that out.

In this blog, we will see how the process of calculating the difference between two dates has been simplified compared to the complex logic of using ticks() function.

Scenario:

Let’s see the scenario of calculating the days remaining for a subscription to expire from today’s date. In many organizations, it’s very important to remain aware of when various licenses are about to expire. The ability to accurately determine the remaining days until a license subscription concludes holds high importance.

Traditionally, this task could be achieved using the ticks() function, which seemed complex compared to the dateDifference() function. By leveraging the dateDifference() function, individuals can effortlessly compute the exact number of days left until a license subscription reaches its termination date. I have created a power automate flow for the creation of Account entity records. Also, I have fields for Start Date and End Date of a License, and also a whole number field for Expiry days field to display the remaining days until the license expires.

Below are the steps for the power automate flow.

  • Creation of Account.

Power Automate

  • Add an action “Update a row” to show the remaining expiration days.

Power Automate

  • Adding the datedifference() function in the Expiry days field.

Power Automate

Expression –

dateDifference(formatDateTime(utcNow(),’yyyy-MM-dd’), formatDateTime(triggerOutputs()?[‘body/cr9a6_enddate’],’yyyy-MM-dd’))

Here is the output of the remaining days for the expiration of the license as per the current date, i.e., 25th August, 2023.

Power Automate

The output is in {days}.{hours}:{minutes}:{seconds} format.

Microsoft Power Platform

Now if we want to show the difference only in days, we can use the split() function to get only days, as can be seen below.

Power Automate

Expression: split(dateDifference(formatDateTime(utcNow(),’yyyy-MM-dd’),formatDateTime(triggerOutputs()?[‘body/cr9a6_enddate’],’yyyy-MM-dd’)),’.’)[0]

Here we get the only days remaining for the expiration of the license by extracting the hours, mins, secs.

Power Automate

Conclusion

In this blog, we explored the dateDifference() function and saw how it simplifies the complex logic for calculating the difference between the two dates.

The post How to use the dateDifference() function in Power Automate first appeared on Microsoft Dynamics 365 CRM Tips and Tricks.