cancel
Showing results for
Did you mean:

## DateDiff (Hours) excluding Weekends and Public Holidays

Hi. I have read this article from 2017: Excluding weekends and holidays in date differences in PowerApps

Most posts related to excluding days in DateDiff point to this article's code.

1. Is there an updated way to do this since 2017 as there were issues opening and closing on a weekend?
UPDATE This appears to be the solution: https://powerusers.microsoft.com/t5/Building-Power-Apps/Share-calculate-working-days-between-two-giv...
2. How do I get the hours difference, not days? Do I simply change "Days' to "Hours" and use (7 * 24) and (5 * 24)?
2 ACCEPTED SOLUTIONS

Accepted Solutions
Community Support

Hi @rwittels :

Do you want to get the number of hours between two dates (excluding holidays and weekends)?

If so please try this solution:

1\I assume there is a holiday list:

``````ClearCollect(
HolidayList,
{Description:"New Year's Day",HolidayDate:Date(2021,1,1)},
{Description:"Birthday of Martin Luther King, Jr.",HolidayDate:Date(2021,1,21)},
{Description:"Washington's Birthday",HolidayDate:Date(2021,2,18)}
)``````

2\I assume the start date time and the end date time are:

``````Set(StartDateTime,Date(2021,1,1)+Time(8,0,0));
Set(EndDateTime,Date(2021,1,22)+Time(16,0,0))``````

3\Then you could use this formula to get the hours between two dates

``````(DateDiff(StartDateTime,EndDateTime,Days)-1)*24
-
+
If(Date(Year(StartDateTime),Month(StartDateTime),Day(StartDateTime)) in HolidayList.Description || Weekday(StartDateTime)=1 || Weekday(StartDateTime)=7 ,0,24-Text(StartDateTime,"[\$-en]HH"))
+
If(Date(Year(EndDateTime),Month(EndDateTime),Day(EndDateTime)) in HolidayList.Description || Weekday(EndDateTime)=1 || Weekday(EndDateTime)=7 ,0,24-Text(EndDateTime,"[\$-en]HH"))``````

The key is to calculate the start and end dates separately, because the start and end dates are not a complete 24 hours

Best Regards,

Bof

Community Support

Hi @rwittels :

``````(DateDiff(StartDateTime,EndDateTime,Days)-1)*24
-
+
If(Date(Year(StartDateTime),Month(StartDateTime),Day(StartDateTime)) in HolidayList.HolidayDate || Weekday(StartDateTime)=1 || Weekday(StartDateTime)=7 ,0,24-Text(StartDateTime,"[\$-en]HH"))
+
If(Date(Year(EndDateTime),Month(EndDateTime),Day(EndDateTime)) in HolidayList.HolidayDate || Weekday(EndDateTime)=1 || Weekday(EndDateTime)=7 ,0,Text(EndDateTime,"[\$-en]HH"))``````

Best Regards,

Bof

8 REPLIES 8
Community Support

Hi @rwittels :

Do you want to get the number of hours between two dates (excluding holidays and weekends)?

If so please try this solution:

1\I assume there is a holiday list:

``````ClearCollect(
HolidayList,
{Description:"New Year's Day",HolidayDate:Date(2021,1,1)},
{Description:"Birthday of Martin Luther King, Jr.",HolidayDate:Date(2021,1,21)},
{Description:"Washington's Birthday",HolidayDate:Date(2021,2,18)}
)``````

2\I assume the start date time and the end date time are:

``````Set(StartDateTime,Date(2021,1,1)+Time(8,0,0));
Set(EndDateTime,Date(2021,1,22)+Time(16,0,0))``````

3\Then you could use this formula to get the hours between two dates

``````(DateDiff(StartDateTime,EndDateTime,Days)-1)*24
-
+
If(Date(Year(StartDateTime),Month(StartDateTime),Day(StartDateTime)) in HolidayList.Description || Weekday(StartDateTime)=1 || Weekday(StartDateTime)=7 ,0,24-Text(StartDateTime,"[\$-en]HH"))
+
If(Date(Year(EndDateTime),Month(EndDateTime),Day(EndDateTime)) in HolidayList.Description || Weekday(EndDateTime)=1 || Weekday(EndDateTime)=7 ,0,24-Text(EndDateTime,"[\$-en]HH"))``````

The key is to calculate the start and end dates separately, because the start and end dates are not a complete 24 hours

Best Regards,

Bof

Thank you for this. I will try it out.

Hi. I am not getting the desired result. Here is my code used in App OnStart:

``````ClearCollect(
HolidayList,
{Description:"New Year's Day",HolidayDate:Date(2021,1,1)},
{Description:"Human Rights Day",HolidayDate:Date(2021,3,22)},
{Description:"Good Friday",HolidayDate:Date(2021,4,2)},
{Description:"Family Day",HolidayDate:Date(2021,4,5)},
{Description:"Freedom Day",HolidayDate:Date(2021,4,27)},
{Description:"International Workers' Day",HolidayDate:Date(2021,5,1)},
{Description:"Youth Day (in South Africa)",HolidayDate:Date(2021,6,16)},
{Description:"National Women's Day",HolidayDate:Date(2021,8,9)},
{Description:"Heritage Day",HolidayDate:Date(2021,9,24)},
{Description:"Day of Reconciliation",HolidayDate:Date(2021,12,16)},
{Description:"Christmas Day",HolidayDate:Date(2021,12,25)},
{Description:"Boxing Day",HolidayDate:Date(2021,12,27)}
);
Set(StartDateTime,Date(2021,4,1)+Time(23,0,0));
Set(EndDateTime,Date(2021,4,6)+Time(01,0,0))``````

I did this as an example because the 2nd of April (Friday) was a public holiday, then the weekend, then Monday the 5th was a public holiday. So 11pm on the 1st to 1am on the 6th should only be 2 hours. Using your code to get the difference, I get 72 hours.

If I use this I should get 24 hours but I get 83:

``````Set(StartDateTime,Date(2021,4,1)+Time(12,0,0));
Set(EndDateTime,Date(2021,4,6)+Time(12,0,0))``````

Am I doing something wrong?

Shouldn't "in HolidayList.Description" be "in HolidayList.HolidayDate"?

Community Support

Hi @rwittels :

I'm so sorry,that is my fault.The "HolidayList.Description" should  be "HolidayList.HolidayDate".

The Formula should be:

``````(DateDiff(StartDateTime,EndDateTime,Days)-1)*24
-
+
If(Date(Year(StartDateTime),Month(StartDateTime),Day(StartDateTime)) in HolidayList.HolidayDate || Weekday(StartDateTime)=1 || Weekday(StartDateTime)=7 ,0,24-Text(StartDateTime,"[\$-en]HH"))
+
If(Date(Year(EndDateTime),Month(EndDateTime),Day(EndDateTime)) in HolidayList.HolidayDate || Weekday(EndDateTime)=1 || Weekday(EndDateTime)=7 ,0,24-Text(EndDateTime,"[\$-en]HH"))``````

Best Regards,

Bof

Thanks. I just don't get the right results.

This gives me 24:

``````Set(StartDateTime,Date(2021,4,1)+Time(23,0,0));
Set(EndDateTime,Date(2021,4,6)+Time(01,0,0))``````

and this gives me 72:

``````Set(StartDateTime,Date(2021,4,1)+Time(12,0,0));
Set(EndDateTime,Date(2021,4,6)+Time(12,0,0))``````

Do you get a different result?

Community Support

Hi @rwittels :

``````(DateDiff(StartDateTime,EndDateTime,Days)-1)*24
-
+
If(Date(Year(StartDateTime),Month(StartDateTime),Day(StartDateTime)) in HolidayList.HolidayDate || Weekday(StartDateTime)=1 || Weekday(StartDateTime)=7 ,0,24-Text(StartDateTime,"[\$-en]HH"))
+
If(Date(Year(EndDateTime),Month(EndDateTime),Day(EndDateTime)) in HolidayList.HolidayDate || Weekday(EndDateTime)=1 || Weekday(EndDateTime)=7 ,0,Text(EndDateTime,"[\$-en]HH"))``````

Best Regards,

Bof

Yes! Thank you so much.

Announcements

#### Announcing | Super Users - 2023 Season 1

Super Users – 2023 Season 1    We are excited to kick off the Power Users Super User Program for 2023 - Season 1.  The Power Platform Super Users have done an amazing job in keeping the Power Platform communities helpful, accurate and responsive. We would like to send these amazing folks a big THANK YOU for their efforts.      Super User Season 1 | Contributions July 1, 2022 – December 31, 2022  Super User Season 2 | Contributions January 1, 2023 – June 30, 2023    Curious what a Super User is? Super Users are especially active community members who are eager to help others with their community questions. There are 2 Super User seasons in a year, and we monitor the community for new potential Super Users at the end of each season. Super Users are recognized in the community with both a rank name and icon next to their username, and a seasonal badge on their profile.    Power Apps  Power Automate  Power Virtual Agents  Power Pages  Pstork1*  Pstork1*  Pstork1*  OliverRodrigues  BCBuizer  Expiscornovus*  Expiscornovus*  ragavanrajan  AhmedSalih  grantjenkins  renatoromao    Mira_Ghaly*  Mira_Ghaly*      Sundeep_Malik*  Sundeep_Malik*      SudeepGhatakNZ*  SudeepGhatakNZ*      StretchFredrik*  StretchFredrik*      365-Assist*  365-Assist*      cha_cha  ekarim2020      timl  Hardesh15      iAm_ManCat  annajhaveri      SebS  Rhiassuring      LaurensM  abm      TheRobRush  Ankesh_49      WiZey  lbendlin      Nogueira1306  Kaif_Siddique      victorcp  RobElliott      dpoggemann  srduval      SBax  CFernandes      Roverandom  schwibach      Akser  CraigStewart      PowerRanger  MichaelAnnis      subsguts  David_MA      EricRegnier  edgonzales      zmansuri  GeorgiosG      ChrisPiasecki  ryule      AmDev  fchopo      phipps0218  tom_riha      theapurva  takolota     Akash17  momlo     BCLS776  Shuvam-rpa     rampprakash  ScottShearer     Rusk  ChristianAbata     cchannon  Koen5     a33ik        AaronKnox        Matren        Alex_10        Jeff_Thorpe        poweractivate        Ramole        DianaBirkelbach        DavidZoon        AJ_Z        PriyankaGeethik        BrianS        StalinPonnusamy        HamidBee        CNT        Anonymous_Hippo        Anchov        KeithAtherton        alaabitar        Tolu_Victor        KRider        sperry1625        IPC_ahaas      zuurg    rubin_boer   cwebb365      If an * is at the end of a user's name this means they are a Multi Super User, in more than one community. Please note this is not the final list, as we are pending a few acceptances.  Once they are received the list will be updated.

#### Microsoft Power Platform Conference | Registration Open | Oct. 3-5 2023

We are so excited to see you for the Microsoft Power Platform Conference in Las Vegas October 3-5 2023! But first, let's take a look back at some fun moments and the best community in tech from MPPC 2022 in Orlando, Florida.   Featuring guest speakers such as Charles Lamanna, Heather Cook, Julie Strauss, Nirav Shah, Ryan Cunningham, Sangya Singh, Stephen Siciliano, Hugo Bernier and many more.   Register today: https://www.powerplatformconf.com/

#### Check out the new Power Platform Communities Front Door Experience!

We are excited to share the ‘Power Platform Communities Front Door’ experience with you!   Front Door brings together content from all the Power Platform communities into a single place for our community members, customers and low-code, no-code enthusiasts to learn, share and engage with peers, advocates, community program managers and our product team members. There are a host of features and new capabilities now available on Power Platform Communities Front Door to make content more discoverable for all power product community users which includes ForumsUser GroupsEventsCommunity highlightsCommunity by numbersLinks to all communities Users can see top discussions from across all the Power Platform communities and easily navigate to the latest or trending posts for further interaction. Additionally, they can filter to individual products as well.       Users can filter and browse the user group events from all power platform products with feature parity to existing community user group experience and added filtering capabilities.     Users can now explore user groups on the Power Platform Front Door landing page with capability to view all products in Power Platform.    Explore Power Platform Communities Front Door today. Visit Power Platform Community Front door to easily navigate to the different product communities, view a roll up of user groups, events and forums.