Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
Nihar Sharma

Subtract (Using Number Field) 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 Estimated_Ship_Week__c(e.g 9/1/2015) - Mfg_Cycle_Time_Target__c (12)(This is Number field) days, it should exclude all the weekends and give me the final date 9/14/2015 .
Please let me know if you have any function related to this requirement.
Thanks for looking into this.

Regards,
Nihar
Best Answer chosen by Nihar Sharma
Abhishek Bansal
Hi Nihar,

I am very sorry for repeating mistakes.

public Date calculateWeekDays(Date EstimatedDate, Integer targetDays){

Integer nonWorkingDays = 0;
Integer currentDay;
for(Integer i=0; i <= finalDate.daysBetween(EstimatedDate); i++) {
if(currentDay == 6 || currentDay == 0) {
nonWorkingDays++;
}
}

Datetime dt = DateTime.newInstance(finalDate.year(), finalDate.month(), finalDate.day());
if(dt.format('E') == 'Sat'){
}
else if(dt.format('E') == 'Sun'){
}

return finalDate;
}

Thanks,
Abhishek

Abhishek Bansal
Hi Nihar,

Like if you are subtracting 12 from 9/1/2015 how can you get 9/14/2015.

Thanks,
Abhishek
Nihar Sharma
Hi Abhishek,

9/14/2015 getting my final result because i am excluding weekend days (like : satureday & sunday)

so, I need this date code.

Regards,
Nihar
Abhishek Bansal
Hi Nihar,

Still not clear with your requirement .
My concern is how can you get 14(9/14/2015) if you subtract 12 from 1(1/9/2015)

Please give a live example by filling value in below fields

1. Estimated_Ship_Week__c (Provide a value with any date)
2. Mfg_Cycle_Time_Target__c (Any number which will be subtracted from above date field)
3. Final date (Your expected result after excluding all weekends)

Thanks,
Abhishek
Nihar Sharma
Hey Abhishek,

let's say :
Release_Date_Target__c = Estimated_Ship_Week__c -
Mfg_Cycle_Time_Target__c;

1) I am getting this :
9/20/2015 = 9/1/2015 - 12;

2) for example (this is what i am expecting) :
9/14/2015 = 9/1/2015 - 12;

so, I need second date result (it is excluding weekend days) (Working day is 5).

I hope this would be understandable.

Regards,
Nihar
Nihar Sharma
Hey so sorry.

its 8/20/2015 = 9/1/2015 - 12

and 8/14/2015 = 9/1/2015 - 12

Now, it should be clear hopefully.

Regards,
Nihar
Abhishek Bansal
Hi Nihar,

public Date calculateWeekDays(Date EstimatedDate, Integer targetDays){

Integer workingDays = 0;
Integer currentDay;
for(Integer i=0; i <= EstimatedDate.daysBetween(finalDate); i++) {
if(currentDay != 6 && currentDay != 0) {
workingDays++;
}
}

Datetime dt = DateTime.newInstance(finalDate.today().year(), finalDate.today().month(), finalDate.today().day());
if(dt.format('E') == 'Sat'){
}
else if(dt.format('E') == 'Sun'){
}

return finalDate;
}

Just pass your Date and Integer in to this method and it will return you the final date.
Let me know if you face any issue .

Thanks,
Abhishek
Abhishek Bansal
Hi Nihar,

There was some mistakes in code which was posted previosly.
Please update the code as follows :

public Date calculateWeekDays(Date EstimatedDate, Integer targetDays){

Integer nonWorkingDays = 0;
Integer currentDay;
for(Integer i=0; i <= finalDate.daysBetween(EstimatedDate); i++) {
if(currentDay == 6 || currentDay == 0) {
nonWorkingDays++;
}
}

Datetime dt = DateTime.newInstance(finalDate.today().year(), finalDate.today().month(), finalDate.today().day());
if(dt.format('E') == 'Sat'){
}
else if(dt.format('E') == 'Sun'){
}

return finalDate;
}

Thanks,
Abhishek
Abhishek Bansal
Hi Nihar,

I am very sorry for repeating mistakes.

public Date calculateWeekDays(Date EstimatedDate, Integer targetDays){

Integer nonWorkingDays = 0;
Integer currentDay;
for(Integer i=0; i <= finalDate.daysBetween(EstimatedDate); i++) {
if(currentDay == 6 || currentDay == 0) {
nonWorkingDays++;
}
}

Datetime dt = DateTime.newInstance(finalDate.year(), finalDate.month(), finalDate.day());
if(dt.format('E') == 'Sat'){
}
else if(dt.format('E') == 'Sun'){
}

return finalDate;
}

Thanks,
Abhishek
This was selected as the best answer
Nihar Sharma
Abhishek,

I am doing this with trigger.

can i use this code to my trigger ?

Regards,
Nihar
Abhishek Bansal
Yes Nihar, you can use this method anywhere in apex class or trigger.
Nihar Sharma
Abhishek,

How can replace my fields with your given code ?
Abhishek Bansal
Nihar,

1. Paste this method in your trigger.
2. Than call the method with following syntax :
Release_Date_Target__c = calculateWeekDays(Estimated_Ship_Week__c, Mfg_Cycle_Time_Target__c);
3. Now you will have the required date value in Release_Date_Target__c variable.

Thanks,
Abhishek
Nihar Sharma
Abhishek,

It is working. Thanks a lot,

One more thing i need to ask, i know this code is for subtracting but if i want addition then what should i have to do ?

let me send code for addition (check it if i am goes wrong then please let me know).

Regards,
Nihar
Nihar Sharma
Here is the code :

public static Date calculateWeekDaysmfgTarget(Date EstimatedDate, Integer targetDays){

Integer nonWorkingDays = 0;
Integer currentDay;
for(Integer i=0; i <= mfgTarget.daysBetween(EstimatedDate); i++) {
if(currentDay == 6 || currentDay == 0) {
nonWorkingDays++;
}
}

Datetime dt = DateTime.newInstance(mfgTarget.year(), mfgTarget.month(), mfgTarget.day());
if(dt.format('E') == 'Sat'){
}
else if(dt.format('E') == 'Sun'){
}

return mfgTarget;
}

Regards,
Nihar
Abhishek Bansal
Great work Nihar :)
Nihar Sharma
Actually it will store regular days (i mean with weekends),

that is why i asked you. i am not sure that it is correct.
Abhishek Bansal
Nihar you are almost correct but there are few things which you need to update.

public static Date calculateWeekDaysmfgTarget(Date EstimatedDate, Integer targetDays){

Integer nonWorkingDays = 0;
Integer currentDay;
for(Integer i=0; i <= EstimatedDate.daysBetween(mfgTarget); i++) {
if(currentDay == 6 || currentDay == 0) {
nonWorkingDays++;
}
}

Datetime dt = DateTime.newInstance(mfgTarget.year(), mfgTarget.month(), mfgTarget.day());
if(dt.format('E') == 'Sat'){
}
else if(dt.format('E') == 'Sun'){
}

return mfgTarget;
}

Nihar Sharma
Ohhhh. got it.

I am doing very silly mistake. only replace that variable in for loop (may i know what is the difference ?)

confusing with only that variable, why we change ? (i was thinking that the arithmatic functionality is same in both case but how would it changed ?)

for subtract :
for(Integer i=0; i <= ReleaseDate.daysBetween(EstimatedDate); i++)

for(Integer i=0; i <= EstimatedDate.daysBetween(mfgTarget); i++)
Abhishek Bansal
Nihar the daysBetween method will return same value in both caes but the difference would of Sign ( - or +).
Since we are using for loop so we must right postive value in i <= smallerDate.DaysBetween(greaterDate)

If we use greaterDate.daysBetween(smallerDate) than it will reurn a negative value and our loop executes for more time.

Hope everything is clear now.

Nihar Sharma
Thank you so much Abhishek.

Now, everything is clear. :)

Thanks again for your valuable time.

Reagrds,
Nihar