cancel
Showing results for
Did you mean:
Responsive Resident

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

Responsive Resident

Thank you for this. I will try it out.

Responsive Resident

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?

Responsive Resident

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

Responsive Resident

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

Responsive Resident

Yes! Thank you so much.

Announcements