web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / DateDiff (Hours) exclu...
Power Apps
Answered

DateDiff (Hours) excluding Weekends and Public Holidays

(0) ShareShare
ReportReport
Posted on by 510

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-given-dates/m-p/167555#M56176
  2. How do I get the hours difference, not days? Do I simply change "Days' to "Hours" and use (7 * 24) and (5 * 24)?
Categories:
I have the same question (0)
  • Verified answer
    v-bofeng-msft Profile Picture
    Microsoft Employee on at

    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
    -
    Sum(ForAll(Sequence(DateDiff(StartDateTime,EndDateTime,Days)-1,1),If(Date(Year(DateAdd(StartDateTime,Value,Days)),Month(DateAdd(StartDateTime,Value,Days)),Day(DateAdd(StartDateTime,Value,Days))) in HolidayList.Description || Weekday(DateAdd(StartDateTime,Value,Days))=1 || Weekday(DateAdd(StartDateTime,Value,Days))=7,1,0)),Value)*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

  • rwittels Profile Picture
    510 on at

    Thank you for this. I will try it out.

  • rwittels Profile Picture
    510 on at

    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?

  • rwittels Profile Picture
    510 on at

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

  • v-bofeng-msft Profile Picture
    Microsoft Employee on at

    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
    -
    Sum(ForAll(Sequence(DateDiff(StartDateTime,EndDateTime,Days)-1,1),If(Date(Year(DateAdd(StartDateTime,Value,Days)),Month(DateAdd(StartDateTime,Value,Days)),Day(DateAdd(StartDateTime,Value,Days))) in HolidayList.HolidayDate || Weekday(DateAdd(StartDateTime,Value,Days))=1 || Weekday(DateAdd(StartDateTime,Value,Days))=7,1,0)),Value)*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

  • rwittels Profile Picture
    510 on at

    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?

  • Verified answer
    v-bofeng-msft Profile Picture
    Microsoft Employee on at

    Hi @rwittels :

    Please try:

    (DateDiff(StartDateTime,EndDateTime,Days)-1)*24
    -
    Sum(ForAll(Sequence(DateDiff(StartDateTime,EndDateTime,Days)-1,1),If(Date(Year(DateAdd(StartDateTime,Value,Days)),Month(DateAdd(StartDateTime,Value,Days)),Day(DateAdd(StartDateTime,Value,Days))) in HolidayList.HolidayDate || Weekday(DateAdd(StartDateTime,Value,Days))=1 || Weekday(DateAdd(StartDateTime,Value,Days))=7,1,0)),Value)*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"))

    1.PNG2.PNG

    Best Regards,

    Bof

  • rwittels Profile Picture
    510 on at

    Yes! Thank you so much.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 600

#2
WarrenBelz Profile Picture

WarrenBelz 478 Most Valuable Professional

#3
Valantis Profile Picture

Valantis 322

Last 30 days Overall leaderboard