Get the difference between two dates EASY

We have all been there, we need to check the difference between 2 dates, and if you ever had to implement this you would need to use some crazy mathematical equations using the ticks() expression. But now..

I’m not sure when this expression got added, but we can now use dateDifference() expression instead of using ticks().

The dateDifference() expression is a powerful tool in Power Automate and Logic Apps for calculating the difference between two dates.

Allowing to easily determine the number of days, months, or years between two dates, which can be useful in a variety of scenarios.

  1. Syntax and Parameters
  2. How to Use
  3. Extracting the Result
    1. Extracting Days
    2. Extracting Hours
    3. Extracting Minutes
    4. Extracting Seconds
  4. Things to Know
  5. Links

Syntax and Parameters

The syntax is easy with only 2 parameters:

dateDifference('<startDate>', '<endDate>')

How to Use

Below is a simple example of how to use this expression:

dateDifference('2015-02-08T10:30:00', '2018-07-30T14:45:30')

This returns

"1268.04:15:30"

The result is in the format of:
Days.Hours:Minutes:Seconds

Note:: If the dates passed in have no time interval, the result shows zeros for the hours, minutes, and seconds. We can extract the different parts of the return by using some expressions inside a Compose action, which we will do next.

Extracting the Result

If you need to extract certain parts of the result into the hours, minutes, or even seconds, you can use the split() expression.
Below you will find the explanation on the extraction, as well as the exact expressions to use.

  • The split() function splits the output of dateDifference() at the period (‘.’) into an array with two elements: days and the rest (hours:minutes:seconds).
  • The [0] indexer retrieves the first element of the array, which represents the number of days.
  • The int() function converts the days from a string to an integer.
  • Replace the date time values with your dates/time

Extracting Days

To extract the days from the result we can use

int(split(dateDifference('2015-02-08T10:30:00', '2018-07-30T14:45:30'), '.')[0])

This returns:

1268

Extracting Hours

To extract the hours interval from the result we can use

int(split(split(dateDifference('2015-02-08T10:30:00', '2018-07-30T14:45:30'), '.')[1], ':')[0])

This returns:

4

Extracting Minutes

To extract the minutes interval from the result we can use

int(split(split(dateDifference('2015-02-08T10:30:00', '2018-07-30T14:45:30'), '.')[1], ':')[1])

This returns:

15

Extracting Seconds

To extract the seconds interval from the result we can use

int(split(split(dateDifference('2015-02-08T10:30:00', '2018-07-30T14:45:30'), '.')[1], ':')[2])

This returns:

30

Things to Know

There are a few things to be aware of:

  • Be aware of time zones, Power Automate uses UTC as a baseline for all time formats.
  • If pulling dates from SharePoint be aware of what time zone your site is in.
  • You can convert the time zones by using expressions or by using actions. Read more about converting time zones here.

date Difference – Reference guide for expression functions – Azure Logic Apps | Microsoft Learn