“I’ve got two times, the task start time and end time, how do I calculate a difference in minutes or hours with Power Automate?”
One of the features missing in Power Automate is a calculation of a difference between two dates / times. There’re many operations with date, you can calculate a date, you can format a date, but you can’t easily calculate a difference. To do that you’ll need a combination of expressions, and that’s what this post is about.
Calculate the number of ticks
As already explained in the post on Excel based reminders, Power Automate has an expression called ticks(…). This expression will take a date and calculate a number of ticks for that date. One tick is 100 nanoseconds, starting with 1st of January 0001 00:00:00. For example, 6th of February 2020 at 19:28 will return 637797689122217300 ticks.
When you calculate a difference between two dates / times, you must get ticks for both of the dates, e.g.
today - yesterday ticks(today) - ticks(yesterday) ticks(utcNow()) - ticks(addDays(utcNow(),-1))
Once you have the two numbers of ticks, you can subtract one from the other with the sub(…) expression.
sub(ticks(utcNow()),ticks(addDays(utcNow(),-1))) = 864000000000
Note: if you end up with a negative number, you should switch the two dates in the expression.
Convert ticks into more readable unit
The result of the subtraction should be then converted into a more understandable number. You don’t want to show users the difference in billions. Use the div(…) expression to divide the number by a number representing the desired unit.
|Divide by||To get|
For example, to get the number of minutes between yesterday and today:
To calculate a difference between two dates / times in Power Automate, you must combine multiple expressions. Calculate the number of ticks for each date, and then divide it by a number to get the desired unit. Start from 100 nanoseconds (1 tick) and divide it by a number big enough to get the days/hours/minutes.
And if you’d like to get the result as a decimal number, apply float(…) on one of the numbers.