cancel
Showing results for
Did you mean:
Memorable Member

## Number of days between two Dates

I'm trying to calculate the number of days between two date/times. A bit of searching tells me there used to be a function for this, dateDiff() but it's now gone. Appears I'm not the only one who's noticed: https://powerusers.microsoft.com/t5/Flow-Ideas/Missing-Calculate-Number-of-days-between-two-Dates/id...

Despite this, I still need to calculate the number of days between two dates. I imagine there are a few ways of doing so. What's the easiest?

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support

Hi @WillPage ,

Please try using the Expression below to calculate the difference between the two dates.

`div(sub(ticks('2019-05-13'),ticks('2019-05-04')),864000000000)`

Please check this blog and refer to the detailed solution:

https://powerusers.microsoft.com/t5/Microsoft-Flow-Community-Blog/Flow-Calculate-Date-Diference/ba-p...

Best Regards,

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
10 REPLIES 10
Community Support

Hi @WillPage ,

Please try using the Expression below to calculate the difference between the two dates.

`div(sub(ticks('2019-05-13'),ticks('2019-05-04')),864000000000)`

Please check this blog and refer to the detailed solution:

https://powerusers.microsoft.com/t5/Microsoft-Flow-Community-Blog/Flow-Calculate-Date-Diference/ba-p...

Best Regards,

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Memorable Member

@v-bacao-msftThanks. I found that thread after posting my question and indeed using ticks() solved the problem.

New Member

This is a nice formula.  We want to use it for employee leave forms.  We want the number of days calculated as working days only.  That means we exclude weekends.  Also, ideally, we would like to also exclude national holidays from this calculation.  Any ideas?

Memorable Member

@pablocaron1745  I suggest, instead of using a formula with ticks() for this, use a Do Until loop.

Define a string variable called day. When you initialise it, set its starting value to be the start day of the leave in yyyy-MM-dd format. Next do a compose with the ending day in the same format.

Next initialise another variable, an integer called working days with a starting value of 0 or blank (null)

Now add an Until loop and the condition is the day variable is equal to the Compose (end date).

Within the Do Until, put a condition. The condition is formatDateTime(variables('day'),'dddd') starts with S.

In the Yes branch, do nothing. In the No branch, increment working days integer variable by 1.

Now, you will need a premium license for this (or pre-populate a SP list with the data) but you need to use the Calendarific API (it's free)

These formulas are:

``````formatDateTime(variables('day'),'dd')
formatDateTime(variables('day'),'MM')
formatDateTime(variables('day'),'yyyy')``````

Now another condition" length(body('Get_public_Holiday_today')?['response']?['holiday']) is equal to 0. In the Yes branch, add one to the working days variable using increment variable. In the No, do nothing.

Now add 1 minute delay (the free trier of calendarific only lets you do one API call per minute - there are workarounds if this is time sensitive).

Lastly, put variables('day') into a Compose called tempDay and next step, Set Variable action with the day variable to addDays(outputs('tempDay'),1,'yyyy-MM-dd).

Outside your Do Until you have an integer variable Working days with the number of working days in that period.

Workaround to calendarific API limitation: Do a one-off flow to get all the holidays in 1 year and put them in an SP list using the "iso" property of the response to fill a string column in SP. In your main flow, Get items with a Odata filter query for  iso eq '@{variables('day')}' and evaluate the length of the response. Calendarific docs here: https://calendarific.com/api-documentation

Anonymous
Not applicable

@WillPage can you show a graphic of the layout for string variable, compose and variable. I keep getting an error message. Thanks

Post Prodigy

I wonder if you could help me with my issue where I am calculating days or hours between a DateReported and Now (UK Time). The Days part is working but the hours isn't it is reporting so strange numbers or hours, if the hours go past a number of days it appears to work but I have seen it report 4 minutes as 7 hours.

Helper I

can you show the steps in power automate flow? I don't get what to put in do until condition.

New Member

This works, thank you!!!

Helper III

can i have this in desktop flow?

New Member

Make use of the dateDifference function either using Set Variable or Compose function.

``int(split(dateDifference('2023-01-01T10:30:00', '2024-01-06T14:45:30'), '.')[0])``

In many cases, you may want to initialize a variable first and then set the variable using the above expression.

Announcements

#### Super User of the Month | Drew Poggemann

As part of a new monthly feature in the Community, we are excited to share that Drew Poggemann is our featured Super User for the month of February 2024. If you've been in the Community for a while, we're sure Drew's name is familiar to you, as he is one of our most active contributors--he's been a Super User for five consecutive seasons!   Since authoring his first reply 5 years ago to his 514th solution authored, Drew has helped countless Community members with his insights and expertise. In addition to being a Super User, Drew is also a User Group leader and a Microsoft MVP. His contributions to our Super User sessions and to the new SUIT program are always welcome--as well as his sense of humor and fun-loving way of sharing what he knows with others.   When Drew is not solving problems and authoring solutions, he's busy overseeing the Solution Architecture team at HBS, specializing in application architecture and business solution strategy--something he's been doing for over 30 years. We are grateful for Drew and the amazing way he has used his talent and skills to help so many others in the Community. If you are part of the SUIT program, you got to hear some great tips from Drew at the first SUIT session--and we know he still has much more to share!You can find him in the Community and on LinkedIn. Thank you for all you do, Drew!

#### Announcing Power Apps Copilot Cookbook Gallery

We are excited to share that the all-new Copilot Cookbook Gallery for Power Apps is now available in the Power Apps Community, full of tips and tricks on how to best use Microsoft Copilot as you develop and create in Power Apps. The new Copilot Cookbook is your go-to resource when you need inspiration--or when you're stuck--and aren't sure how to best partner with Copilot while creating apps.   Whether you're looking for the best prompts or just want to know about responsible AI use, visit Copilot Cookbook for regular updates you can rely on--while also serving up some of your greatest tips and tricks for the Community. Our team will be reviewing posts using the new "Copilot Studio" label to ensure we highlight and amplify the most relevant and recent content, so you're assured of high-quality content every time you visit. If you share a post that gets featured in the curated gallery, you'll get a PM in the Community to let you know!The curated gallery is ready for you to experience now, so visit the new Copilot Cookbook for Power Apps today: Copilot Cookbook - Power Platform Community. We can't wait to see what you "cook" up!

#### Celebrating a New Season of Super Users with Charles Lamanna, CVP Microsoft Business Applications

February 8 was the kickoff to the 2024 Season One Super User program for Power Platform Communities, and we are thrilled to welcome back so many returning Super Users--as well as so many brand new Super Users who started their journey last fall. Our Community Super Users are the true heroes, answering questions, providing solutions, filtering spam, and so much more. The impact they make on the Communities each day is significant, and we wanted to do something special to welcome them at our first kickoff meeting of the year.   Charles Lamanna, Microsoft CVP of Business Applications, has stressed frequently how valuable our Community is to the growth and potential of Power Platform, and we are honored to share this message from him to our 2024 Season One Super Users--as well as anyone who might be interested in joining this elite group of Community members.     If you want to know more about Super Users, check out these posts for more information today:    Power Apps: What is A Super User? - Power Platform CommunityPower Automate: What is A Super User? - Power Platform Community Copilot Studio: What is A Super User? - Power Platform Community Power Pages: What is A Super User? - Power Platform Community