How to Calculate difference between two dates in Power Automate

Hello everyone and welcome to my blog. In today’s blog we will discuss on how to calculate the difference between two dates in Power Automate.

Sometime back I have written a blog which show how to substract two date time objects and then calculate the difference. Infact most of the blogs available on this topic will demonstrate similar methods.

While it served the purpose, it was rather crude. Power Automate now has a function dateDifference which makes the job easier and we no longer need to use the ticks function. So let’s start with a simple example.

Below is the dateDifference function in action. I have used the dateDifference function to substract two dates.

And below is the output when I run the flow.

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

Let’s make it a little more complex. Let’s test with actual date time fields. In the below example I am substracting modifiedon and createdon fields of DataVerse both of which are DateTime fields.

And below is the output

Here you have all the values for days/ hours/ mins/ seconds. That’s great. But to how to find out days, hours, mins and seconds from the output value.

For example, below is the formula which will give you the “Difference in Days

You use the split function on the Dot (.) character and take the first item. That is your Difference in Days. How do you calculate Difference in hours? Your formula should be something like below

Number of Days * 24 + Number of hours

We already have calculated the Difference in Days. We declare another variable to take the time portion from the output. Below is the formula to do the same.

split(outputs('Compose'),'.')[1]

Remember the output of dateDifference function is in the format of {days}.{hours}:{minutes}:{seconds} ? So we split on the Dot(.) operator one more time and then take the second item from the array. That will give us the time part.

The final part is obviously to take the hours value from the time portion and below is the formula to do that.

add(mul(int(variables('varDiffInDays')),24),int(split(variables('varTimePart'),':')[0]))

We are summing up Difference in day *24 with the hour value from the time portion. I am using the int() function to typecast the string value to integer. And when I run the flow, below is the output.

Hope you liked this post. If this post has helped, you can buy me a coffee.

For similar topics on Microsoft.NET and Power Platform, subscribe to my blog using the Subscribe option on right pane.

Debajit Dutta
Business Solutions MVP