How to Get the Number of Days Between Two Dates in Power Automate

Do you need to calculate the number of days between two dates in Power Automate?

Calculating the number of days between two dates is a common requirement in business applications and workflows.

It is possible to get the number of days between two dates in Power Automate using various functions.

This article will guide you through two expressions to calculate the number of days between two dates. You will learn how to calculate the difference between two dates with the ticks function and the dateDifference function.

Get the Days Between Two Dates with the Ticks Function

The ticks function provides a way to calculate the difference between two dates by converting each date into a numeric representation known as ticks. A tick represents 100-nanosecond intervals, and you can use this information to find the differences in seconds, minutes, hours, or days.

Syntax of the Ticks Function

ticks('<timestamp>')

The ticks function will return the number of 100 nanosecond intervals between a datetime <timestamp> and 0001-01-01 12:00:00 midnight.

Days Between Two Dates with the Ticks Function

Since the ticks function works with nanoseconds, you will need to convert the results into days.

You can convert the results from ticks to days by dividing the number of ticks by 864000000000.

1 day = 24 hours/day * 60 minutes/hour * 60 seconds/minute * 1,000,000,000 nanoseconds/second
1 day = 86,400,000,000,000 nanoseconds

This is because there are 24 hours per day, 60 minutes in an hour, 60 seconds in an hour, and 1,000,000,000 nanoseconds in a second.

This means 1 day has 86,400,000,000,000 nanoseconds.

1 day = 86,400,000,000,000 nanoseconds / (100 nanoseconds/tick)
1 day = 864,000,000,000 ticks

A tick is 100 nanoseconds, therefore 1 day has 864,000,000,000 ticks.

Now you can get the number of ticks between the start and end date and divide by 864,000,000,000 to get the number of days.

div(sub(ticks(outputs('EndDate')),ticks(outputs('StartDate'))),864000000000)

The above expression will give you the number of days between the StartDate and EndDate timestamps.

  • ticks(outputs('EndDate')) gets the number of ticks between the EndDate timestamp and 0001-01-01 12:00:00 midnight.
  • ticks(outputs('StartDate')) gets the number of ticks between the StartDate timestamp and 0001-01-01 12:00:00 midnight.
  • sub(ticksEndDate,ticksStartDate) will get the number of ticks between the two dates.
  • div(ticks,864000000000) this divides the number of ticks between the two dates by 864000000000 to convert it to the number of days between the two dates.

This expression results in the total number of days between the two dates.

Get the Days Between Two Dates with the dateDifference Function

Another way to calculate the difference between two dates in Power Automate is by using the dateDifference function.

Syntax of the dateDifference Function

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

The dateDifference function directly calculates the difference between two date and time timestamps and returns a timespan string for the difference in days, hours, minutes, and seconds.

  • startDate is the starting timestamp.
  • endDate is the ending timestamp.

Days Between Two Dates with the dateDifference Function

dateDifference(outputs('StartDate'),outputs('EndDate'))

The above expression can be used to get the difference between the StartDate and EndDate.

This returns the timespan string in the format dd.hh:mm:ss. In the above example, it returns 25 days as 25.00:00:00.

Get Days as Integer from Timespan String

The dateDifference function will return a string value that includes the number of days, but you might only want the day part of this value as an integer data type.

This is possible with a few more functions to parse the day value and convert it to an integer.

int(split(dateDifference(outputs('StartDate'),outputs('EndDate')), '.')[0])

The above expression will return the number of days between the StartDate and EndDate as an integer.

  • split(timespan, '.') will split the timespan string at the period character and return an array with the split values.
  • split()[0] will return the first part of the split text from the split array. This will contain the number of days.
  • int() will convert the number of days string to an integer data type.

Conclusions

Calculating the number of days between two dates in Power Automate is a common task and it’s possible with two different expressions in Power Automate.

The ticks expression requires converting ticks to days by dividing the results by 864000000000, the number of ticks in a day, to obtain the number of days between the two dates.

The dateDiffenence function is more straightforward but will require you to parse the number of days from the results with the split function.

Incorporating these techniques into your Power Automate workflows will allow you to handle complex date related tasks.

I hope this information helps with your flow. Let me know in the comments!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

Related Articles

Comments

4 Comments

  1. Sebastian

    When comparing two dates on the same day you need to use ticks(), as dateDifference() does not return 0 when the date is on the same day, rather it just returns hours and second. So the suggested use of split() “int(split( dateDifference( {now}, {then} ), ‘.’)[0])” will result in an error as the result will not have a “0 days” as expected with the format “days.hh:mm:ss.milliseconds” rather it just omits the days and returns “hh:mm:ss.milliseconds”

    Reply
    • John MacDougall

      Thanks for the tips! I had not considered same day timestamps.

      Reply
  2. Marko

    also when the 2 dates are the same
    the convert to int bugs because it can’t convet 00:00:00 to int

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Get the Latest Tech Tips

Write For Us

Are you a tech enthusiast with a talent for writing great content? Come write for us!

Follow Us

Follow us on social media to stay up to date with the latest in tech!