function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
karthikeya 2karthikeya 2 

how to calculate number of holidays between two Dates

Hi all , 
I had a requirement to calcualte number of holidays betwwn two days using Business hours Holidays.
Any one can provide the code for it.


Thanks in advance.
sandhya reddy 10sandhya reddy 10
Hi Karthikeya,

Please try below soql  which will return no of holidays that are saved in your company profile.

Holidays is a standard object available in salesforce from which u can get no of holidays.

​Date s = date.newInstance(2014,04,01); //Get your start and end dates however you want
Date e = date.newInstance(2014,12,31);
Holiday[] h = [Select ID From Holiday Where ActivityDate >= :s AND ActivityDate <= :e];
Integer numOfHolidays = h.size();

Please let us know if this helps you.

Thanks and Regards
sandhya reddy
karthikeya 2karthikeya 2
HI sandhya,

i have 5 different Business hours. so ineed to pic perticular business hours.
 
in businesses hours i have the following

1) Default
2) US hours
3) INDIA hours
4) USI Hours
5) TRT hours

out of these i need to pic only "US hours" related holidays. and its count.
The code you are ging is working but its picking from all the business hours.
 
sandhya reddy 10sandhya reddy 10
Hi Karthikeya,

use this code
 
public list<Holiday> holidaysmethod()
{
​Date s = date.newInstance(2014,04,01); //Get your start and end dates however you want
Date e = date.newInstance(2014,12,31);
Holiday[] listHoliday= [Select ID From Holiday Where ActivityDate >= :s AND ActivityDate <= :e];
Integer numOfHolidays = h.size();

for(BusinessHours objBusinessHours: [select Id, Name From BusinessHours]) {
                    for(Holiday objHoliday: listHoliday) {

                        Datetime HolidayDate = DateTime.newInstance( objHoliday.ActivityDate.year(), objHoliday.ActivityDate.month(), objHoliday.ActivityDate.day(), 0 , 0 , 0 );

                        if( BusinessHours.isWithin(objBusinessHours.Id, HolidayDate) ) {
                            if( objBusinessHours.Name == 'US ' ) {
                                listJapanHoliday.add(objHoliday);
                            }
                        }
                    }
                }
return listJapanHoliday;

}
sandhya reddy 10sandhya reddy 10
Thanks and Regards
sandhya
vahidkhan lodhivahidkhan lodhi
Hello sandhya,

Its give me error's.
vahidkhan lodhivahidkhan lodhi
User-added image
James DearmerJames Dearmer
@sandhya reddy 10 -- This suggestion does not incorporate users taking leave on the weekends.