Skip to main content

Notifications

How to find the number of days between two dates using Power Automate

One question that is frequently asked on the Power Automate discussion boards is how to find the difference between two dates.  There is not currently a built-in Power Automate expression that will get the job done.  To find out ho to accomplish this requirement, read on...

 

In my scenario, I will want to find the difference in days between a date stored in a SharePoint list and the current date. 

Here's what we are going to do:

  1. Convert the two dates to ticks
    1. A tick is a small faction on a second
  2. Subtract one date from the other
    1. The result will be the difference in ticks
  3. Convert the ticks to days
    1. You're done!

Step 1 - Convert to ticks

Use the ticks() expression to convert both dates to ticks

image.pngimage.png

Step 2 - Get the difference in ticks

Use the sub() expression to find the difference between the two dates in ticks

image.png

 

Step 3 - Convert the difference in ticks to days

There are 864000000000 ticks in a day.  Divide the difference in ticks by 864000000000 to find the difference in days

image.png

 

I suspect that the Power Automate team will eventually give us an expression to find the difference between two dates.  Until then, use the technique outlined above.

Comments

*This post is locked for comments

  • VSTrath Profile Picture VSTrath 25
    Posted at
    How to find the number of days between two dates using Power Automate

    @ScottShearer - I tried to use dateDifference('<@{items('Apply_to_each')?['Start_Date']}>', '<@{utcNow()}>') in an update list item action but it is throwing an error asking for a valid number. I need to work out number of days since registration so I can trigger reminder emails after 6 months and 12 months. Any advice?

     

     

     

  • VSTrath Profile Picture VSTrath 25
    Posted at
    How to find the number of days between two dates using Power Automate

    Are those Set variable actions with the purple icon? Thanks

  • ScottShearer Profile Picture ScottShearer 25,150
    Posted at
    How to find the number of days between two dates using Power Automate

    I want everyone to know that, while the technique above still works, we now have a dateDifference() expression.

    dateDifference() returns a timestamp, so you'll still need to extract the number of days - I like to use a split() expression for this.

  • 七蚊雞 Profile Picture 七蚊雞 12
    Posted at
    How to find the number of days between two dates using Power Automate

    Thanks OP for this thread and PHCMA for your help! This has really helped me. I'd like to elaborate on PHCMA's reply, because it took me awhile to understand what 'dateSubtractingFrom' and 'dateSubtractingWith' meant.

     

    Say you have startDate (June 1) and endDate (June 3), where you want to find the number of days between startDate and endDate. In that case, 'dateSubtractingFrom' is the endDate, and 'dateSubtractingWith' is startDate. 

     

    I also had to get my dates from a Form response, so I had to remove variables. Put together, my expression looked like this:

    int(div(sub(ticks(outputs('Get_response_details')?['body/r....']),ticks(outputs('Get_response_details')?['body/r....'])),864000000000))

     

    Hope this would help someone in the future. 🙂 

  • PHCMA Profile Picture PHCMA 41
    Posted at
    How to find the number of days between two dates using Power Automate

    Thanks for this, here's the compacted version I used in Set Variable

     

     

    div(sub(ticks(variables('dateSubtractingFrom')),ticks(variables('dateSubtractingWith'))),864000000000)

     

     

    Wrap with 

    int( )

    if you need the variable in integer format.

  • curious1 Profile Picture curious1
    Posted at
    How to find the number of days between two dates using Power Automate

    I would try looking at the security certificates and trying https it might be the solution