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
Priyanka PallepatiPriyanka Pallepati 

Subtract business days(exclude weekends) from a given date

Hi all - 

Using Apex, did you ever had to subtract the business days from any given date? 
Like for ex: if I have to subtract from today 11/19/2014-10 days, it should exclude all the weekends and give me the final date 11/5/2014.
Please let me know if you have any function related to this requirement.
Thanks for looking into this.

Regards,
Priyanka
BalajiRanganathanBalajiRanganathan

I have done similar to this before. please try the steps below.

1) Under Setup -> Company Profile-> Business Hours, create a new business hours which has no hours for Week ends.

2) Using Apex do the following
  
 BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name = '<your new Business hours>'];
 Long duration = inputDays * -86400000;
 BusinessHours.addGmt(bh.id, <Your_Date__c>, duration);

replace <your new Business hours> with your business hour name. also note that 86400000 is number of milling seconds per day and using negative operation as you want to subtract.
Priyanka PallepatiPriyanka Pallepati
Hi Balaji - 

I looked at the business hours under setup and I see this

Default: Sunday24 HoursMonday24 HoursTuesday24 HoursWednesday24 HoursThursday24 HoursFriday24 HoursSaturday24 Hours

So do I have to create a new business hours which says Sunday 24 Hours, Saturday 24 Hours? 

What is inputDays? is it the no of days that needs to be subtracted(in my above example(11/19/2014-10 days) --- inputDays is 10??

And <Your_Date__c> is the inputDate(in my above example(11/19/2014-10 days) -- <Your_Date__c> is 11/19/2014?

How does this work? I am confused. 

Please let me know.
BalajiRanganathanBalajiRanganathan
Yes, you have to create new Business Hour so that you will not touch any other process which is using default business hours.
while creating new business hours, select the active check box, uncheck the 24 hours and clear the time in the Saturday and Sunday. once you save it will show as No Hours for Saturday and Sunday. all other days will show 24 hours

yes, you understanding is correct on the input days.

For  <Your_Date__c>, you need to pass DateTime field. if you want to use current date, then use System.now()

BusinessHours.addGmt(bh.id, System.now(), duration);

 
Priyanka PallepatiPriyanka Pallepati
Hi Balaji - 

I created new Business Hour. Its No Hours for Saturday and Sunday and the rest, 24 hours.
I am querying my bh by this:
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE Name = '<your new Business hours>'];
I have the duration as this: Long duration = 10 * -86400000;(considering inputDays = 10)
My date time is this(dt): 2014-12-27 05:00:00 ( a Saturday).

When I put a debug statement like this: system.debug('---business add gmt----'+BusinessHours.addGmt(bh.Id, dt, duration));

Its showing me in the logs as 
19:48:54.088 (88938481)|USER_DEBUG|[116]|DEBUG|---business add gmt----2014-12-13 05:00:00

2014-12-27 - 10 days( excluding weekends) is 2014-12-15.. But BusinessHours.addGmt(bh.Id, dt, duration)); is showing as 2014-12-13 in my case?

Do you know why?

Thanks,
Priyanka


 
BalajiRanganathanBalajiRanganathan
1) Can you verify your calender if saturday and sunday has no hours not some thing like Sunday and Monday.
2) Also make sure that you dont pass the hours to the datetime just pass the date.
 for example 
BusinessHours.addGmt(bh.id, datetime.newInstance(2014, 12, 27), duration)
Priyanka PallepatiPriyanka Pallepati
Hi Balaji-

1) Pretty sure the calendar is saturday and sunday no hours. I even debug all the weekdays and weekend start and end time and it says saturday and sunday as null null and others with times.
2) I tried BusinessHours.addGmt(bh.id, datetime.newInstance(2014, 12, 27), duration) with no hours. still the same result 
3) I tried BusinessHours.add(bh.id, datetime.newInstance(2014, 12, 27), duration) took out addGmt and instead tried add.. still the same result..:(


 
BalajiRanganathanBalajiRanganathan
can you post you full code? are you executing using ananymous window?. i executed my end and i am getting the correct result as 2014-12-15 for the sample you have given?
Priyanka PallepatiPriyanka Pallepati
 String Date='12/27/2014'
Date custDeliveryDate = Date.parse(Date);
Datetime dt = datetime.newInstanceGmt(custDeliveryDate.year(), custDeliveryDate.month(),custDeliveryDate.day());
                 system.debug('---dt----'+dt);
                BusinessHours bh = [SELECT Id,SundayEndTime,SundayStartTime,SaturdayEndTime,SaturdayStartTime FROM BusinessHours WHERE Name = 'Schedule Email Business Hours'];
                Long duration = 10 * -86400000;
              system.debug('---duration----'+duration);
               system.debug('---sunday---'+bh.SundayStartTime+bh.SundayEndTime);
               system.debug('---saturday---'+bh.SaturdayStartTime+bh.SaturdayEndTime);
                //BusinessHours.addGmt(bh.Id, dt, duration);
                BusinessHours.addGmt(bh.id, datetime.newInstance(2014, 12, 27), duration);
           system.debug('---business add gmt----'+BusinessHours.add(bh.id, datetime.newInstance(2014, 12, 27), duration));
Priyanka PallepatiPriyanka Pallepati
I am not using anonymous window. I have this code in my controller.
Priyanka PallepatiPriyanka Pallepati
My business hours:

Business Hours Name Schedule Email Business Hours
Time Zone (GMT-06:00) Central Standard Time (America/Chicago)

Business Hours

Sunday No Hours
Monday 24 Hours
Tuesday 24 Hours
Wednesday 24 Hours
Thursday 24 Hours
Friday 24 Hours
Saturday No Hours

Default Business Hours [Not Checked]
Active [Checked]

 
BalajiRanganathanBalajiRanganathan
I did not see any problem with your code and i got the below when i execute with the timezone you had.
business add gmt----2014-12-15 06:00:00

1) execute the code you copied in the ananymous window and see the result
2) check what is the API version in your controller class.
Priyanka PallepatiPriyanka Pallepati
Hi - Just wanted to confirm .. Is this same for you?(Time Zone GMT as below)?


Business Hours Name Schedule Email Business Hours
Time Zone (GMT-06:00) Central Standard Time (America/Chicago)

Business Hours

Sunday No Hours
Monday 24 Hours
Tuesday 24 Hours
Wednesday 24 Hours
Thursday 24 Hours
Friday 24 Hours
Saturday No Hours

Default Business Hours [Not Checked]
Active [Checked]
 
Priyanka PallepatiPriyanka Pallepati
Version is 28.0.. What is your class version?
BalajiRanganathanBalajiRanganathan
I tested only ananymous window. the latest version is 32.0
Priyanka PallepatiPriyanka Pallepati
I changed it to 32.0 and tested it in the controller as well as anonymous.
Tough luck. Shows the same as 13th dec and 15th dec
Priyanka PallepatiPriyanka Pallepati
shows as 13th dec.. not as 15th dec ****
juppyjuppy
I know this is an old post, however I have just had a requirement to add a given number of business days to a start date, and solved it as follows. Hope it helps someone.
The set of holiday dates can be constructed from custom settings, the Holiday object or wherever.
You can also easily adjust for cultures where working days are not Mon - Fri
Cheers
Mike
 
public static date addWorkingDays(date dtIn, set<date> set_Holidays, integer iBusinessDaysToAdd)
{
	integer iDaysToAdd = 0;
	integer iBusinessDaysFound = 0;

	// Start with day after start date
	datetime dttmIn = (datetime)dtIn + 1;

	do
	{
		if (integer.valueOf(dttmIn.format('u')) < 6 && !set_Holidays.contains(date.valueOf(dttmIn)))
		{
			iBusinessDaysFound += 1;
		}
			
		dttmIn = dttmIn.addDays(1);
		iDaysToAdd += 1;
	}
	while (iBusinessDaysFound < iBusinessDaysToAdd);

	return dtIn.addDays(iDaysToAdd);
}
Millar DakkimMillar Dakkim
Hello, payday loans online in Michigan (https://maybeloan.com/payday-loans/mi) needs some amount of money to start. Getting a consumer loan has become quite simple nowadays. You need to fulfill a few basic steps, and then you will quickly bring it on.
Emma James 9Emma James 9
Some money is required to get started with payday loans online in West Virginia (https://easyqualifymoney.com/payday-loans-west-virginia.php). A consumer loan application has grown a lot easier in recent years. After completing a few simple steps, you'll be able to rapidly bring it on.
Zenni JamesZenni James
Have unexpected expenses? Just apply for an online payday loan. We've compiled a list of payday loan lenders who offer short-term loans to people with all types of credit. Check here: https://easyqualifymoney.com/online-payday-loan-lenders.php
Drop trackDrop track
Payday loan regulations vary across provinces in payday loans canada (https://infopaydayloans.com/) . Familiarize yourself with the specific laws and regulations in your province to ensure that you are fully aware of your rights and protections as a borrower.