cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Akhlaq
Regular Visitor

How do we calculate the difference between two dates and times in power automate?

I tried to get the difference between two dates and times using Power automate. But the output only comes individually in Days or Hours or Minutes or Seconds.

 

We need the below output in Hours and minutes (HH: mm) using power automate:

Suppose, if we subtract from (End Date & Time) 20/11/2022 13:05 to (Start Date & Time) 20/11/2022 01:30.

Output must come in the format "13:35" (13 hours, 35 minutes).

 

Any help please?

1 ACCEPTED SOLUTION

Accepted Solutions
RobElliott
Super User
Super User

@Akhlaq unlike with Power Apps, with Power Automate there is no datediff function so we need to calculate the difference with a series of compose actions. I have a list with date/time columns called Start and End and I've added the date and times you put into your post (although the correct difference is 11 hours 35, not 13 hours 35 😁 ).
0-SP-List.png

 

1-Flow.png

 

I've got the item from Sharepoint and there then need to be 2 compose actions using ticks. The first one, ticksStart, uses the expression ticks(outputs('Get_item')?['body/Start'])

 

The second, ticksEnd, uses the expression ticks(outputs('Get_item')?['body/End'])

2-Flow.png

 

Next, another compose action calculates the difference in minutes with the expression 
div(div(mul(sub(outputs('ticksEnd'), outputs('ticksStart')), 100), 1000000000), 60)

 

Next, the difference in hours is calculated with another expression
div(outputs('DifferenceMinutes'),60)

 

3-Flow.png

 

To make sure that the minutes part of the result is returned as minutes and not as a percentage we need to use the mod function in another compose with the expression
mod(outputs('DifferenceMinutes'),60)

 

The result in the format hh:mm is created with yet another compose action using the expression
concat(outputs('DifferenceHours'),':',outputs('Mod'))

 

4-Flow.png

 

which gives this result when the flow runs and you can use the output of that for whatever other actions you need.
5-Result.png

 

Rob
Los Gallardos
If I've answered your question or solved your problem, please mark this question as answered. This helps others who have the same question find a solution quickly via the forum search. If you liked my response, please consider giving it a thumbs up. Thanks.

 

View solution in original post

2 REPLIES 2
RobElliott
Super User
Super User

@Akhlaq unlike with Power Apps, with Power Automate there is no datediff function so we need to calculate the difference with a series of compose actions. I have a list with date/time columns called Start and End and I've added the date and times you put into your post (although the correct difference is 11 hours 35, not 13 hours 35 😁 ).
0-SP-List.png

 

1-Flow.png

 

I've got the item from Sharepoint and there then need to be 2 compose actions using ticks. The first one, ticksStart, uses the expression ticks(outputs('Get_item')?['body/Start'])

 

The second, ticksEnd, uses the expression ticks(outputs('Get_item')?['body/End'])

2-Flow.png

 

Next, another compose action calculates the difference in minutes with the expression 
div(div(mul(sub(outputs('ticksEnd'), outputs('ticksStart')), 100), 1000000000), 60)

 

Next, the difference in hours is calculated with another expression
div(outputs('DifferenceMinutes'),60)

 

3-Flow.png

 

To make sure that the minutes part of the result is returned as minutes and not as a percentage we need to use the mod function in another compose with the expression
mod(outputs('DifferenceMinutes'),60)

 

The result in the format hh:mm is created with yet another compose action using the expression
concat(outputs('DifferenceHours'),':',outputs('Mod'))

 

4-Flow.png

 

which gives this result when the flow runs and you can use the output of that for whatever other actions you need.
5-Result.png

 

Rob
Los Gallardos
If I've answered your question or solved your problem, please mark this question as answered. This helps others who have the same question find a solution quickly via the forum search. If you liked my response, please consider giving it a thumbs up. Thanks.

 

@RobElliott Thanks for the solution. I used your solution but got the same formatting issue i.e (HH: mm required). For example (06:56), if we calculate the difference between two dates and times, i.e from 21-11-2022 22:00 to 21-11-2022 20:00. After calculation, the output came in the format "2:0". Please see the below screenshot for your reference. The output should be "02:00". 

Thanks!

 

Akhlaq_1-1669048736474.png

 

 

 

 

 

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Users online (3,189)